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>