Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 學習筆記:用 Bundle 在 Activity 之間傳值

Bundle 是什麼

Bundle 是 Android 用來在不同 Activity 之間傳遞資料的容器,本質上是一組 key-value 映射,並可序列化,因此能隨 Intent 一起送過來。

常用方法

  • clear():清除此 Bundle 中保存的所有資料。
  • clone():複製目前的 Bundle。
  • containsKey(String key):判斷是否含有指定 key。
  • getString(String key):取出指定 key 的字串值。
  • hasFileDescriptors():是否含有打包的檔案描述符。
  • isEmpty():Bundle 是否為空。
  • putString(String key, String value):放入字串值。
  • remove(String key):移除指定 key。
  • readFromParcel / writeToParcel:與 Parcel 之間的讀寫。

送出端:Android2Activity.java

package Android2.test;

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 Android2Activity extends Activity {

    private EditText et = null;
    private Button button = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et = (EditText)findViewById(R.id.edit);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new ButtonListener());
    }

    public class ButtonListener implements OnClickListener{

        public void onClick(View arg0) {
            Bundle bundle = new Bundle();
            bundle.putString("Name", et.getText().toString());
            Intent intent = new Intent(Android2Activity.this, bundle.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"/>
</LinearLayout>

接收端:bundle.java

package Android2.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class bundle extends Activity{
    private TextView text = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bundle);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        text = (TextView)findViewById(R.id.text);
        text.setText(bundle.getString("Name"));
    }
}

bundle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

兩個實作上的小提醒:接收端最好用 containsKey 先判斷,或直接用 getString("Name", "") 的重載版本,避免 key 不存在時拿到 null 造成空指針;另外別忘了在新的 Activity 於 AndroidManifest.xml 中註冊,否則會 ActivityNotFoundException。Bundle 適合傳小型資料,若要傳大量內容,應改用其他機制。

Copyright © Linux教程網 All Rights Reserved