FirstActivity.java
1 import android.os.Bundle; 2 import android.view.View; 3 import android.view.View.OnClickListener; 4 import android.widget.Button; 5 import android.app.Activity; 6 import android.content.Intent; 7 8 public class FirstActivity extends Activity { 9 private Button myButton; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 System.out.println("FirstActivity ---> onCreate "); 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_first); 15 myButton = (Button) findViewById(R.id.myButton); 16 myButton.setOnClickListener(new ButtonListener()); 17 } 18 19 @Override 20 protected void onDestroy() { 21 System.out.println("FirstAcvity --->onDestory"); 22 super.onDestroy(); 23 } 24 25 @Override 26 protected void onPause() { 27 System.out.println("FirstAcvity --->onPause"); 28 super.onPause(); 29 } 30 31 @Override 32 protected void onRestart() { 33 System.out.println("FirstAcvity --->onRestart"); 34 super.onRestart(); 35 } 36 37 @Override 38 protected void onResume() { 39 System.out.println("FirstAcvity --->onResume"); 40 super.onResume(); 41 } 42 43 @Override 44 protected void onStart() { 45 System.out.println("FirstAcvity --->onStart"); 46 super.onStart(); 47 } 48 49 @Override 50 protected void onStop() { 51 System.out.println("FirstAcvity --->onStop"); 52 super.onStop(); 53 } 54 55 56 class ButtonListener implements OnClickListener{ 57 58 @Override 59 public void onClick(View v) { 60 Intent intent = new Intent(); 61 intent.setClass(FirstActivity.this, SecondActivity.class); 62 FirstActivity.this.startActivity(intent); 63 } 64 65 } 66 }
activity_activity.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello_world" 11 /> 12 <Button 13 android:id="@+id/myButton" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="@string/first_button" 17 /> 18 </LinearLayout>
SecondActivity.java
1 import android.app.Activity; 2 import android.content.Intent; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.view.View.OnClickListener; 6 import android.widget.Button; 7 8 public class SecondActivity extends Activity{ 9 private Button secondButton; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 System.out.println("SecondActivity--->onCreate"); 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_second); 15 16 secondButton = (Button)findViewById(R.id.secondButton); 17 secondButton.setOnClickListener(new ButtonListener()); 18 } 19 20 21 @Override 22 protected void onDestroy() { 23 System.out.println("SecondActivity--->onDestory"); 24 super.onDestroy(); 25 } 26 27 @Override 28 protected void onPause() { 29 System.out.println("SecondActivity--->onPause"); 30 super.onPause(); 31 } 32 33 @Override 34 protected void onRestart() { 35 System.out.println("SecondActivity--->onRestart"); 36 super.onRestart(); 37 } 38 39 @Override 40 protected void onResume() { 41 System.out.println("SecondActivity--->onResume"); 42 super.onResume(); 43 } 44 45 @Override 46 protected void onStart() { 47 System.out.println("SecondActivity--->onStart"); 48 super.onStart(); 49 } 50 51 @Override 52 protected void onStop() { 53 System.out.println("SecondActivity--->onStop"); 54 super.onStop(); 55 } 56 57 58 class ButtonListener implements OnClickListener{ 59 60 @Override 61 public void onClick(View v) { 62 Intent intent = new Intent(); 63 intent.setClass(SecondActivity.this, FirstActivity.class); 64 SecondActivity.this.startActivity(intent); 65 } 66 67 } 68 }
activity_second.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="wrap_content" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 /> 11 <Button 12 android:id="@+id/secondButton" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:text="@string/second_button" 16 /> 17 </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mars.activity05" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mars.activity05.FirstActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="SecondActivity" android:theme="@android:style/Theme.Dialog"/> <!--android:theme="@android:style/Theme.Dialog"/>,表示将这个Activity的格式设置为,对话框的形式--> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Activity05</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="first_button">FirstButton</string> <string name="second_button">SecondButton</string> </resources>