Activity01
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity01 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /* 设置显示main.xml布局 */
Button button = (Button)this.findViewById(R.id.button1);
button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//新建一个Intent
Intent intent = new Intent();
//制定intent要启动的类
intent.setClass(Activity01.this, Activity02.class);
//启动一个新的Activity
startActivity(intent);
//关闭当前的
Activity01.this.finish();
}
});
}
}
Activity02
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity02 extends Activity{ /*savedInstanceState 保存当前Activity的状态信息。*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //调用父类的onCreate构造函数)
/* 设置显示main2.xml布局 */
setContentView(R.layout.main2);
/* findViewById(R.id.button2)取得布局main.xml中的button2 */
Button button = (Button) findViewById(R.id.button2);
/* 监听button的事件信息 */
button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity02.this, Activity01.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity02.this.finish();
}
});
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">第一个Activity</string>
<string name="hello2">第二个Activity</string>
<string name="app_name">Activity切换</string>
</resources>
![](http://hi.csdn.net/attachment/201202/27/0_1330314583TH8i.gif)
main.xml
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_x = "120px"
android:layout_y = "90px"
android:text ="切换"
android:id="@+id/button1"
>
</Button>
</LinearLayout>
![](http://hi.csdn.net/attachment/201202/27/0_1330314711e1gg.gif)
main2.xml
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello2"
/>
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_x = "120px"
android:layout_y = "90px"
android:text ="切换"
android:id="@+id/button2"
>
</Button>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lysine"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Activity01"
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="Activity02">
</activity>
</application>
</manifest>