Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android Intent 物件的基礎用法

編輯:進階開發

如果要換的不只是背景顏色或文字,而是整個 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>

二、IntentDemo.java

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
            }
        });
    }
}

三、IntentDemo1.java

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 都能達到類似效果,差別在於你要的是明確指定元件,還是讓系統替你挑一個合適的處理者。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved