編輯:初階開發
Android 用 Intent 這個特殊類別來完成 Activity 之間的切換。透過解析各種 Intent,從一個畫面導航到另一個畫面變得很簡單;向前導航時,Activity 會呼叫 startActivity(Intent myIntent)。
下面用一個完整範例示範:在首頁輸入文字,按下按鈕後跳到第二個畫面,並把文字帶過去顯示。
package intent.pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentTest extends Activity {
/** Called when the activity is first created. */
private EditText et1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(R.id.editText1);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View arg0) {
String val = et1.getText().toString();
// 生成一個 Intent 對象
Intent intent = new Intent();
// 設置傳遞的參數
intent.putExtra("val", val);
// 從 Activity IntentTest 跳轉到 Activity IntentTest01
intent.setClass(IntentTest.this, IntentTest01.class);
// 啟動 intent 的 Activity
IntentTest.this.startActivity(intent);
}
}
}
package intent.pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class IntentTest01 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent01);
Intent intent = getIntent();
String value = intent.getStringExtra("val");
TextView et = (TextView) findViewById(R.id.textView1);
et.setText(value);
}
}
目標頁面用 getIntent() 取得啟動自己的 Intent,再用 getStringExtra("val") 取出對應的鍵值,這裡的 key 必須與出發頁面 putExtra 時用的名稱一致。
<?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"
>
<EditText android:id="@+id/editText1"
android:layout_width="match_parent"
android:text="請輸入內容!"
android:layout_height="wrap_content"
>
</EditText>
<Button android:id="@+id/button1"
android:text="確定"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
</Button>
</LinearLayout>
<?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:id="@+id/textView1"
android:textSize="20sp"
android:layout_width="match_parent"
android:text="請輸入內容!"
android:layout_height="wrap_content"
></TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="intent.pack"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4"
/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
>
<activity android:name=".IntentTest"
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=".IntentTest01"
android:label="@string/app1_name">
</activity>
</application>
</manifest>
範例中有個地方最容易出錯:新的 Activity 一定要在 AndroidManifest.xml 中宣告,否則執行時會直接拋出 ActivityNotFoundException。只有帶有 MAIN / LAUNCHER 意圖過濾器的那一個 Activity 會成為啟動圖示,其餘 Activity 僅需簡單宣告即可。
另外,Intent 傳值時 key 是字串比對,建議為常用的 key 定義常數,避免拼字錯誤造成難以追查的問題。
Android 繪圖學習總結(三):認識 Drawable
Android SDK 提供了一個功能強大的抽象類別 Drawable。它究竟代表什麼、又該怎麼用?最有效率的方式是從簡單範例動手理解。 一、用 ShapeDrawab
Android GridView 網格版面實作:自定義 ImageAdapter
先前提到過 GridView 也是一種 AdapterView,和 ListView 頗為相似。實際用過一輪之後有些心得,在此分享。 思路 1. 既然與 ListVie
保護你的 Android 付費軟體——LVL 全攻略(一)
近來 Google 開始大力打擊 Android 軟體盜版,為開發者提供了 License Verification Library(LVL)機制,以防止軟體被盗版濫用。
Android 特色開發:帳戶管理(AccountManager)實作解析
本文節選自機械工業出版社《Android 應用開發揭秘》,作者楊豐盛。該書涵蓋 Android 框架、組件、使用者介面、遊戲開發、資料儲存、多媒體與網路開發等基礎,並深入