1.介紹
用於不同Activity之間的數據傳遞
2.重要方法
clear():清除此Bundle映射中的所有保存的數據。
clone():克隆當前Bundle
containsKey(String key):返回指定key的值
getString(String key):返回指定key的字符
hasFileDescriptors():指示是否包含任何捆綁打包文件描述符
isEmpty():如果這個捆綁映射為空,則返回true
putString(String key, String value):插入一個給定key的字符串值
readFromParcel(Parcel parcel):讀取這個parcel的內容
remove(String key):移除指定key的值
writeToParcel(Parcel parcel, int flags):寫入這個parcel的內容
Android2Activity.java
[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) {
- // TODO Auto-generated method stub
- 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
[java]
- <?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" >
- <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
[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
[java]
- <?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:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>