How to switch between screens?

后端 未结 6 1241
梦如初夏
梦如初夏 2020-12-31 20:37

I\'m new in the android develop world. I created simple application and created a simple GUI with one button. If the user presses this button, I want to change the screen

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 21:09

    A better word than "Screen" is "Activity", You switch between Activities in android.

    A very simple way is to create a button on one activity (Lets call this First Activity) and assign it a method like onClick = startSecondActivity in the .xml file.

    Open FirstActivity.java, Add the method startSecondActivity inside the main method as shown below. This will work!

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Set the content of the activity to use the activity_main.xml layout file
            setContentView(R.layout.activity_main);
        }
    
        public void startSecondActivity(View view){
            Intent i = new Intent(this, SecondActivity.class);
            startActivity(i);
        }
    }
    

提交回复
热议问题