Two activity switching examples

Source: Internet
Author: User
Activity01 Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; </P> <p> public class activity01 extends activity {</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main);/* set to display main. XML Layout */</P> <p> button = (button) This. findviewbyid (R. id. button1); <br/> button. setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view v) {<br/> // create a new intent <br/> intent = new intent (); <br/> // specifies the intent class to be started. <br/> intent. setclass (activity01.this, activity02.class); <br/> // start a new activity <br/> startactivity (intent ); <br/> // close the current <br/> activity01.this. finish (); </P> <p >}< br/>}); <br/>}</P> <p>}

Activity02

Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; </P> <p> public class activity02 extends activity {/* savedinstancestate saves the status information of the current activity. */</P> <p> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); // call the oncreate constructor of the parent class) <br/>/* set to display the main2.xml Layout */<br/> setcontentview (R. layout. main2); <br/>/* findviewbyid (R. id. button2) Get the layout main. button2 */<br/> button = (button) findviewbyid (R. id. button2); <br/>/* listen to button event information */<br/> button. setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view v) {<br/>/* Create an intent object */<br/> intent = new intent (); <br/>/* specify the intent class to start */<br/> intent. setclass (activity02.this, activity01.class); <br/>/* start a new activity */<br/> startactivity (intent ); <br/>/* close the current activity */<br/> activity02.this. finish (); <br/>}</P> <p >}); <br/>}< br/>}

Strings. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> </P> <p> <string name = "hello"> first activity </string> <br/> <string name = "hello2"> second activity </string> <br/> <string name = "app_name"> activity switchover </string> </P> <p> </resources>

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical"> </P> <p> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> <button <br/> Android: layout_width = "100px" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_x = "120px" <br/> Android: layout_y = "90px" <br/> Android: TEXT = "Switch" <br/> Android: id = "@ + ID/button1" <br/> </button> </P> <p> </linearlayout>

Main2.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical"> </P> <p> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/hello2" <br/> <button <br/> Android: layout_width = "100px" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_x = "120px" <br/> Android: layout_y = "90px" <br/> Android: TEXT = "Switch" <br/> Android: id = "@ + ID/button2" <br/> </button> </P> <p> </linearlayout>

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "CN. lysine "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> </P> <p> <uses-SDK Android: minsdkversion = "7"/> </P> <p> <application <br/> Android: icon = "@ drawable/ic_launcher" <br/> Android: label = "@ string/app_name"> <br/> <activity <br/> Android: Name = ". activity01 "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> </P> <p> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> </P> <p> <activity <br/> Android: name = "activity02"> <br/> </activity> </P> <p> </Application> </P> <p> </manifest>

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.