Android之Bundle类
API文檔說(shuō)明
1.介紹
用于不同Activity之間的數(shù)據(jù)傳遞
1.重要方法
clear():清除此Bundle映射中的所有保存的數(shù)據(jù)。
clone():克隆當(dāng)前Bundle
containsKey(String key):返回指定key的值
getString(String key):返回指定key的字符
hasFileDescriptors():指示是否包含任何捆綁打包文件描述符
isEmpty():如果這個(gè)捆綁映射為空,則返回true
putString(String key, String value):插入一個(gè)給定key的字符串值
readFromParcel(Parcel parcel):讀取這個(gè)parcel的內(nèi)容
remove(String key):移除指定key的值
writeToParcel(Parcel parcel, int flags):寫(xiě)入這個(gè)parcel的內(nèi)容
官方文檔
http://developer.android.com/reference/android/os/Bundle.html
實(shí)例
public class BundleDemo extends Activity {private EditText etName;Button btn;/** (non-Javadoc)* * @see android.app.Activity#onCreate(android.os.Bundle)*/@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.bundle);etName = (EditText) findViewById(R.id.etname);btn = (Button) findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic 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)*/@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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的區(qū)別
SharedPreferences是簡(jiǎn)單的存儲(chǔ)持久化的設(shè)置,就像用戶每次打開(kāi)應(yīng)用程序時(shí)的主頁(yè),它只是一些簡(jiǎn)單的鍵值對(duì)來(lái)操作。它將數(shù)據(jù)保存在一個(gè)xml文件中
Bundle是將數(shù)據(jù)傳遞到另一個(gè)上下文中或保存或回復(fù)你自己狀態(tài)的數(shù)據(jù)存儲(chǔ)方式。它的數(shù)據(jù)不是持久化狀態(tài)。
參考鏈接
Android之Bundle傳遞數(shù)據(jù)詳解與實(shí)例及Bundle與SharedPreferences的區(qū)別 - ForrestWoo - 博客園
Android Bundle類 - randyjiawenjie的專欄 - 博客頻道 - CSDN.NET
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Android之Bundle类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简单GDB调试
- 下一篇: android sina oauth2.