FirstActivity.java
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent; public class FirstActivity extends Activity {
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("FirstActivity ---> onCreate ");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
} @Override
protected void onDestroy() {
System.out.println("FirstAcvity --->onDestory");
super.onDestroy();
} @Override
protected void onPause() {
System.out.println("FirstAcvity --->onPause");
super.onPause();
} @Override
protected void onRestart() {
System.out.println("FirstAcvity --->onRestart");
super.onRestart();
} @Override
protected void onResume() {
System.out.println("FirstAcvity --->onResume");
super.onResume();
} @Override
protected void onStart() {
System.out.println("FirstAcvity --->onStart");
super.onStart();
} @Override
protected void onStop() {
System.out.println("FirstAcvity --->onStop");
super.onStop();
} class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
FirstActivity.this.startActivity(intent);
} }
}
activity_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_button"
/>
</LinearLayout>
SecondActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class SecondActivity extends Activity{
private Button secondButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("SecondActivity--->onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); secondButton = (Button)findViewById(R.id.secondButton);
secondButton.setOnClickListener(new ButtonListener());
} @Override
protected void onDestroy() {
System.out.println("SecondActivity--->onDestory");
super.onDestroy();
} @Override
protected void onPause() {
System.out.println("SecondActivity--->onPause");
super.onPause();
} @Override
protected void onRestart() {
System.out.println("SecondActivity--->onRestart");
super.onRestart();
} @Override
protected void onResume() {
System.out.println("SecondActivity--->onResume");
super.onResume();
} @Override
protected void onStart() {
System.out.println("SecondActivity--->onStart");
super.onStart();
} @Override
protected void onStop() {
System.out.println("SecondActivity--->onStop");
super.onStop();
} class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(SecondActivity.this, FirstActivity.class);
SecondActivity.this.startActivity(intent);
} }
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/secondButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_button"
/>
</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>