編輯:進階開發
如果要換的不只是背景顏色或文字,而是整個 Activity 都被替換掉,那單靠改 Layout 就不夠了——尤其當你需要傳遞變數,而手機上又沒有 Cookie 或 Session 這種網頁世界的機制時,僅僅切換內容檢視顯然無法把主導權交給另一個 Activity。
這正是 Intent 存在的理由。Intent 顧名思義就是「意圖」:在主 Activity 裡宣告自己是誰、想去哪裡,交由系統安排。以下用與前一篇相同的效果,示範 Intent 的基本用法。
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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="歡迎來到魏祝林的部落格" />
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊進入 Layout2" />
</LinearLayout>
mylayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog" />
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊進入 Layout1" />
</LinearLayout>
package com.android.test;
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 {
private Button bt1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 建立 Intent 並指定要啟動的 Class
Intent intent = new Intent();
intent.setClass(IntentDemo.this, IntentDemo1.class);
startActivity(intent); // 啟動新的 Activity
IntentDemo.this.finish(); // 關閉目前的 Activity
}
});
}
}
package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo1 extends Activity {
private Button bt2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
bt2 = (Button) findViewById(R.id.bt2);
bt2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(IntentDemo1.this, IntentDemo.class);
startActivity(intent);
IntentDemo1.this.finish();
}
});
}
}
本例的關鍵在最後一步:新加的 Activity 必須在 AndroidManifest.xml 中宣告並給定名稱,否則程式根本無法執行。這個問題幾乎每個新手都會遇到一次。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.android.test"
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="IntentDemo1" />
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
補充一點:這裡用 finish() 把原頁面關掉,所以按返回鍵時不會回到上一頁,適合閃屏或引導頁那種單向流程。若想保留返回路徑,把 finish() 拿掉即可,Activity 會自動進入返回堆疊。而 setClass 只是 Intent 眾多寫法之一——用 ComponentName、Action 或 URI 都能達到類似效果,差別在於你要的是明確指定元件,還是讓系統替你挑一個合適的處理者。
市面上的Android手機,真的熱賣嗎?
Android手機的核心系統服務建立在Linux 2.6核心之上,安全性、記憶體管理、行程管理、網路協議疊與驅動模型都由此提供,而這些又統一由一個工作管理器協調,用起来既
Monkey 壓力測試全解析:Android 應用穩定性的第一道防線
Android Chrome 瀏覽器是運行於模擬器或裝置上的一個程式,盡力為用戶提供良好的 Web 體驗,其穩定性與智慧程度在業界屬一流水準。而要驗證這種穩定度,Monk
Win7 建置 Android 開發環境(6):下載 SDK 並設定路徑
安裝 ADT 時,一路按 Next、接受授權協議,最後會自動完成安裝。 四、下載 Android SDK 選擇 android-sdk_r10-windows.zip。
Eclipse 開發 Android 入門(7):建立 AVD 並把程式跑起來
在 Android SDK and AVD Manager 視窗中點擊 New,填入以下設定: Name: android_1.6 Target: Goo