在一個Android應用中可以包含零個或多個Acivity。當你的應用中包含多個Activity時,通常要在各個Activity中間跳轉。在Android中,完成這些操作需要使用Intent的組件。
理解這個既重要又抽象概念的最好辦法,就是嘗試一下。下面的例子展示如何在兩個Activity之間跳轉。
1.建立一個名為UsingIntent的工程。
2.建立兩個Activity:UsingIntentActivity和SecondActivitty。
3.AndroidManifest.xml中的代碼。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.horsttnann.UsingIntent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".UsingIntentActivity"
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="Second Activity" >
<intent-filter>
<action android:name="net.horsttnann.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
4.在res/layout檔案夾下,建立一個叫secondactivity.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="This is the Second Activity!" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter your name" />
<EditText
android:id="@+id/txt_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="OK" />
</LinearLayout>
5.SecondActivity中的代碼。package net.horsttnann.UsingIntent;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
}
}
6.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" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Display second activity" />
</LinearLayout>
7.UsingIntentActivity中的代碼。package net.horsttnann.UsingIntent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class UsingIntentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
startActivity(new Intent("net.horsttnann.SecondActivity"));
}
}
8.按F11調試。 www.2cto.com
:
提示:
Intent-Filter中的anction屬性盡量使用反轉網域名稱,這樣就能減少被其他使用相同action的程式啟動的幾率。
Intent-Filter中的category屬性是android.intent.category.DEFAULT。只有添加了這個屬性,這個Activity才能被另外一個Activity使用startActivity()方法來開啟。
還有另外一種啟動Activity的方法,但如果想使用這個方法,就必須確保這兩個Activity在同一個工程下面。
startActivity(new Intent(this, SecondActivity.class));
作者 manoel的專欄