实现步骤 :
第一步 :建立Android 工程:IntentDemo。
第二步 :编写Activity 的子类别:IntentDemo,其程序代码如下:
package com.a3gs.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main );
Button btn1 = (Button) findViewById(R.id.btn1 );
btn1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(IntentDemo.this , Intent2.class );
/* 调用一个新的 Activity */
startActivity(intent);
/* 关闭原本的 Activity */
IntentDemo.this .finish ();
}
});
}
}
第三步 :新建一个Activity 的子类别:Intent2,其程序代码如下:
package com.a3gs.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Intent2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.mylayout );
Button btn2 = (Button) findViewById(R.id.btn2 );
btn2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Intent2.this , IntentDemo.class );
startActivity(intent);
Intent2.this .finish ();
}
});
}
}
第四步 :修改res/layout/main.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/text1"
/>
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bt1"
/>
</LinearLayout>
第四步 :修改res/layout/mylayout.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/text2"
/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bt2"
/>
</LinearLayout>
第五步 :修改AndroidManifest.xml,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a3gs.intent"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IntentDemo"
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=".Intent2" android:label="@string/app_name"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
扩展学习 :
当系统中新添加 Activity时,必须在 AndroidManifest.xml里定义一个新的 activity:
<activity android:name=".Intent2"></activity>否则系统将会因为找不到 Activity而发生编译错误。
另外,当程序中出现两个以上的 Activity时,系统要如何决定主程序是哪一支( entry point)呢?以本范例来说, AndroidManifest.xml中 Activity IntentDemo的定义如下:
<activity android:name=".IntentDemo" android:label = "@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
其中有一行为 <category android:name="android.intent.category.LAUNCHER" />,这就代表程序启动时,会先运行 IntentDemo这个 Activity,而非 Intent2。需注意的是,这个参数必须要被定义,如果 xml中没有一支 Activity有设置这个参数,则程序将不会被运行。
此外,在两支 Java程序中的最后一行都调用了 finish() 这个方法,它代表这个 Activity已运作完毕,当系统接收到这个命令时,即会关闭此 Activity,所以此时点击模拟器的返回( Back)键,并不会回到上一个 Activity的画面,如果要让模拟器的返回键有回上一页的效果,可以将此行程序注释掉。同理,当两个 Activity在切换时,并非真的只有两个 Activity在切换,而是在点击按钮时,再重新调用起一个新的 Activity。