Android之Bundle类
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的內容
官方文檔
http://developer.android.com/reference/android/os/Bundle.html
實例
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的區別
SharedPreferences是簡單的存儲持久化的設置,就像用戶每次打開應用程序時的主頁,它只是一些簡單的鍵值對來操作。它將數據保存在一個xml文件中
Bundle是將數據傳遞到另一個上下文中或保存或回復你自己狀態的數據存儲方式。它的數據不是持久化狀態。
參考鏈接
Android之Bundle傳遞數據詳解與實例及Bundle與SharedPreferences的區別 - ForrestWoo - 博客園
Android Bundle類 - randyjiawenjie的專欄 - 博客頻道 - CSDN.NET
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Android之Bundle类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单GDB调试
- 下一篇: OpenCV中的傅里叶的门道