Android interface switching method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.