You can switch between the two methods:
Method 1: Use setContentView to switch to layout, which is commonly used. Perform the following steps:
① Create an xml file for layout on the Interface
② Trigger a control (such as Button). The control has loaded the listener. The Listener switches to layout through the setContentView function.
The whole process of this implementation is implemented on an Activity. All variables are in the same State, so all variables can be obtained in this Activity state.
The Code is as follows:
[Java]
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button button = (Button) this. findViewById (R. id. button1 );
// Set the listener for the button
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Call the setContentView function to switch to layout.
SetContentView (R. layout. login );
}
});
}
Method 2: convert to another Activity by performing the following steps:
① Create an Activity class
② Register the class to AndroidManifest. xml, as shown below:
<Activity android: name = ". LoginActivity"> </activity>
③ Create an Intent class on the original Activity to switch the Activity. The Code is as follows:
[Java]
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button button_activity = (Button) this. findViewById (R. id. button2 );
Button_activity.setOnClickListener (new OnClickListener ()){
@ Override
Public void onClick (View v ){
Intent intent = new Intent (MainActivity. this, LoginActivity. class );
StartActivity (intent );
}
});
}
Author: chenshijun0101