Android jump between two activities

Prepare Java classes

MainActivity Java class code

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity {


    private Button button ;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
//button点击跳转
button.setOnClickListener(new myListener());
}                                        
    

    class myListener implements CompoundButton.OnClickListener{
        @Override
public void onClick(View view) {
            Intent intent = new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);
MainActivity.this.startActivity(intent);
}                                        
    }
}

Corresponding xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a1.activity01.MainActivity">
    <Button
android:id="@+id/btn"
android:layout_width=                    
                "match_parent"
android:layout_height="wrap_content"
android:text="点击跳转"/>
</LinearLayout>                

OtherActivity Java class code

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Created by 1号检测室 on 2018/4/16.
 */

public class OtherActivity extends AppCompatActivity {
    private TextView tv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_main);
        tv=findViewById(R.id.tv);
        tv.setText(R.string.other);
    }
}

对应的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

AndroidManifest.xml文件添加新的OtherActivity.java

<activity android:name=".OtherActivity"
android:label="@string/other"/>    
ok

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324519188&siteId=291194637