Android studio implements simple page jump

Use intent components to implement simple jumps

home page

Button button1,button2,button3; //xml文件定义的id
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);

        //跳转游戏界面
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,GameActivity.class); //跳转到的activity文件
                //页面跳转
                startActivity(intent);
            }
        });

        //跳转排行榜界面
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this,PaiHangActivity.class); 
                //页面跳转
                startActivity(intent2);
            }
        });

        //跳转设置界面
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent(MainActivity.this,SettingsActivity.class);
                //页面跳转
                startActivity(intent3);
            }
        });
    }

Page after jump (return function)

    Button back2; //xml定义的返回按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);      

        back2 = findViewById(R.id.back2);

        //返回按钮跳转
        Intent intent1 = new Intent(this,MainActivity.class);    //绑定返回主页面
        back2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                startActivity(intent1);
            }
        });
    }

At this point, you can jump and return between pages. At that time, I thought it was over when I first did it. As a result, I found an error when I ran it. Later I found that I had neglected to add the newly created page to the configuration file. Here everyone also Be careful ⚠️⚠️⚠️

Add the newly created page to AndroidMainfest.xml

        <activity android:name=".GameActivity"
            android:exported="true">

        </activity>

At this point, a simple page jump can be achieved.

Guess you like

Origin blog.csdn.net/ZcRook1e/article/details/131280712