最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式

 更新時間:2017年03月20日 09:51:56   作者:呆萌先生  
這篇文章主要介紹了關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

使用Inten的putExtra傳遞

第一個Activity中

//創(chuàng)建意圖對象
 Intent intent = new Intent(this,TwoActivity.class);
 //設(shè)置傳遞鍵值對
 intent.putExtra("data",str);
 //激活意圖
 startActivity(intent);

第二個Activity中

// 獲取意圖對象
 Intent intent = getIntent();
 //獲取傳遞的值
 String str = intent.getStringExtra("data");
 //設(shè)置值
 tv.setText(str);

使用Intention的Bundle傳遞

第一個Activity中

//創(chuàng)建意圖對象
 Intent intent = new Intent(MainActivity.this,TwoActivity.class);
 //用數(shù)據(jù)捆傳遞數(shù)據(jù)
 Bundle bundle = new Bundle();
 bundle.putString("data", str);
 //把數(shù)據(jù)捆設(shè)置改意圖
 intent.putExtra("bun", bundle);
 //激活意圖
 startActivity(intent);

第二個Activity

//獲取Bundle
 Intent intent = getIntent();
 Bundle bundle = intent.getBundleExtra("bun");
 String str = bundle.getString("data");
 tv.setText(str);

使用Activity銷毀時傳遞數(shù)據(jù)

第一個Activity中

  Intent intent = new Intent(MainActivity.this,TwoActivity.class);
  //用一種特殊方式開啟Activity
 startActivityForResult(intent, 11);
//設(shè)置數(shù)據(jù)
 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 String str = data.getStringExtra("data");
 tvOne.setText(str);
}

第二個activity中

//設(shè)置返回的數(shù)據(jù)
 Intent intent = new Intent();
 intent.putExtra("data", edtOne.getText().toString().trim());
 setResult(3, intent);
 //關(guān)閉當前activity
 finish();

SharedPreferences傳遞數(shù)據(jù)

第一個Activity中

SharedPreferences sp = this.getSharedPreferences("info", 1);
 //獲取sp編輯器
 Editor edit = sp.edit();
 edit.putString("data", str);
 edit.commit();
 //創(chuàng)建意圖對象
 Intent intent = new Intent(MainActivity.this,TwoActivity.class);
 //激活意圖
 startActivity(intent);

第二個Activity中

SharedPreferences sp = this.getSharedPreferences("info", 1);
 //設(shè)置數(shù)據(jù)
 tv.setText(sp.getString("data", ""));

使用序列化對象Seriazable

工具類

import java.io.Serializable;
class DataBean implements Serializable {
 private String name;
 private String sex;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getSex() {
 return sex;
 }
 public void setSex(String sex) {
 this.sex = sex;
 }
}

第一個Activity

//創(chuàng)建意圖
 Intent intent = new Intent(MainActivity.this,TwoActivity.class);
 DataBean bean = new DataBean();
 //通過set方法把數(shù)據(jù)保存到DataBean對象中
 bean.setName("啦啦");
 bean.setSex("男");
 intent.putExtra("key", bean);
 startActivity(intent);

第二個Activity

Intent intent = getIntent();
 //反序列化數(shù)據(jù)對象
 Serializable se = intent.getSerializableExtra("key");
 if(se instanceof DataBean){
  //獲取到攜帶數(shù)據(jù)的DataBean對象db
  DataBean db = (DataBean) se;
  tv.setText(db.getName()+"==="+db.getSex());
 }

使用靜態(tài)變量傳遞數(shù)據(jù)

第一個Activity

Intent intent = new Intent(MainActivity.this,TwoActivity.class);
  TwoActivity.name="牛逼";
  TwoActivity.str="你說";
  startActivity(intent);

第二個Activity

//靜態(tài)變量
protected static String name;
protected static String str;
tv.setText(str+name);

以上所述是小編給大家介紹的關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

涟水县| 威海市| 莆田市| 富宁县| 黔江区| 石楼县| 江北区| 兴隆县| 新余市| 宜黄县| 宜都市| 靖西县| 海口市| 定日县| 崇文区| 宁津县| 宜兴市| 繁昌县| 博兴县| 七台河市| 连城县| 江都市| 吕梁市| 宣武区| 平凉市| 天峻县| 眉山市| 盱眙县| 台南市| 鄢陵县| 大化| 南皮县| 静乐县| 潮州市| 阿鲁科尔沁旗| 光泽县| 庄浪县| 濮阳市| 敦煌市| 云和县| 隆子县|