Create an Android project with the following code:
Main. xml
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">"
<TextView android: id = "@ + id/textView"
Android: text = "click to Activity2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">"
<TextView android: id = "@ + id/textView"
Android: text = "click to Activity2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout> HelloWorldActivity. java
[Java] public class HelloWorldActivity extends Activity implements OnClickListener {
Private Button button;
/* Inherits the Activity and must implement the onCreate method.
* Every Activity must be registered in AndroidManifest. xml,
**/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button = (Button) findViewById (R. id. button );
Button. setText ("My Button ");
Button. setOnClickListener (this );
}
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (HelloWorldActivity. this, Activity2.class );
// Start Activity2
HelloWorldActivity. this. startActivity (intent );
}
}
Public class HelloWorldActivity extends Activity implements OnClickListener {
Private Button button;
/* Inherits the Activity and must implement the onCreate method.
* Every Activity must be registered in AndroidManifest. xml,
**/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button = (Button) findViewById (R. id. button );
Button. setText ("My Button ");
Button. setOnClickListener (this );
}
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (HelloWorldActivity. this, Activity2.class );
// Start Activity2
HelloWorldActivity. this. startActivity (intent );
}
}
Create a class Activity2:
Activity2.xml
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "activy2"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "activy2"/>
</LinearLayout> Activity2.java
[Java] public class Activity2 extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity2 );
}
}
Public class Activity2 extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity2 );
}
}
Register Acivity2 in AndroidManifest. xml:
[Html] <activity android: name = ". Activity2"
Android: label = "activity2"/>
<Activity android: name = ". Activity2"
Android: label = "activity2"/>
The running effect is as follows. If you press return, the system returns to HelloWorldActivity,
From Hu HU's column