效果如下:

初始界面

輸入信息

“確定”以后
一、API文檔說明
1.介紹
用于不同Activity之間的數據傳遞
1.重要方法
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的內容
二、實例
public class BundleDemo extends Activity {
private EditText etName;
Button btn;
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle);
etName = (EditText) findViewById(R.id.etname);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String info = etName.getText().toString();
Bundle bundle = new Bundle();
//保存輸入的信息
bundle.putString("name", info);
Intent intent=new Intent(BundleDemo.this,BundleDemo1.class);
intent.putExtras(bundle);
finish();
startActivity(intent);
}
});
}
}
public class BundleDemo1 extends Activity {
private TextView etName;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b1);
etName=(TextView)findViewById(R.id.txtname);
Bundle b=getIntent().getExtras();
//獲取Bundle的信息
String info=b.getString("name");
etName.setText("您的姓名:"+info);
}
}
三、與SharedPreferences的區別
SharedPreferences是簡單的存儲持久化的設置,就像用戶每次打開應用程序時的主頁,它只是一些簡單的鍵值對來操作。它將數據保存在一個xml文件中
Bundle是將數據傳遞到另一個上下文中或保存或回復你自己狀態的數據存儲方式。它的數據不是持久化狀態。
標簽:
Android2.2
一、使用Intent
在 Android 中,不同的 Activity 實例可能運行在一個進程中,也可能運行在不同的進程中。因此我們需要一種特別的機制幫助我們在 Activity 之間傳遞消息。Android 中通過 Intent 對象來表示一條消息,一個 Intent 對象不僅包含有這個消息的目的地,還可以包含消息的內容,這好比一封 Email,其中不僅應該包含收件地址,還可以包含具體的內容。對于一個 Intent 對象,消息“目的地”是必須的,而內容則是可選項。
在上面的實例中通過 Activity. startActivity(intent)啟動另外一個 Activity 的時候,我們在 Intent 類的構造器中指定了“收件人地址”。
如果我們想要給“收件人”Activity 說點什么的話,那么可以通過下面這封“e-mail”來將我們消息傳遞出去:
- Intent intent =new Intent(CurrentActivity.this,OtherActivity.class);
-
- Bundle bundle =new Bundle();
- bundle.putBoolean("boolean_key", true);
- bundle.putString("string_key", "string_value");
- intent.putExtra("key", bundle);
- startActivity(intent);
那么“收件人”該如何收信呢?在 OtherActivity類的 onCreate()或者其它任何地方使用下面的代碼就可以打開這封“e-mail”閱讀其中的信息:
- Intent intent =getIntent();
- Bundle bundle =intent.getBundleExtra("key");
- bundle.getBoolean("boolean_key");
- bundle.getString("string_key");
上面我們通過 bundle對象來傳遞信息,bundle維護了一個 HashMap<String, Object>對象,將我們的數據存貯在這個 HashMap 中來進行傳遞。但是像上面這樣的代碼稍顯復雜,因為 Intent 內部為我們準備好了一個 bundle,所以我們也可以使用這種更為簡便的方法:
- Intent intent =new Intent(EX06.this,OtherActivity.class);
- intent.putExtra("boolean_key", true);
- intent.putExtra("string_key", "string_value");
- startActivity(intent);
接收:
- Intent intent=getIntent();
- intent.getBooleanExtra("boolean_key",false);
- intent.getStringExtra("string_key");
二、使用SharedPreferences
SharedPreferences 使用 xml 格式為 Android 應用提供一種永久的數據存貯方式。對于一個 Android 應用,它存貯在文件系統的/data/ data/your_app_package_name/shared_prefs/目錄下,可以被處在同一個應用中的所有 Activity 訪問。Android 提供了相關的 API 來處理這些數據而不需要程序員直接操作這些文件或者考慮數據同步問題。
-
- SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
- Editor editor = preferences.edit();
- editor.putBoolean("boolean_key", true);
- editor.putString("string_key", "string_value");
- editor.commit();
-
-
- SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
- preferences.getBoolean("boolean_key", false);
- preferences.getString("string_key", "default_value");
三、其他方式
Android 提供了包括 SharedPreferences 在內的很多種數據存貯方式,比如 SQLite,文件等,程序員可以通過這些 API 實現 Activity 之間的數據交換。如果必要,我們還可以使用 IPC 方式。
http://blog.csdn.net/sdlgxxy/article/details/6226127