欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

使用Inten的putExtra傳遞

第一個(gè)Activity中

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

第二個(gè)Activity中

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

使用Intention的Bundle傳遞

第一個(gè)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);

第二個(gè)Activity

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

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

第一個(gè)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);
}

第二個(gè)activity中

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

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

第一個(gè)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);

第二個(gè)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;
 }
}

第一個(gè)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);

第二個(gè)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ù)

第一個(gè)Activity

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

第二個(gè)Activity

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

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

相關(guān)文章

  • Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼

    Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼

    本篇文章主要介紹了Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android如何實(shí)現(xiàn)社交應(yīng)用中的評論與回復(fù)功能詳解

    Android如何實(shí)現(xiàn)社交應(yīng)用中的評論與回復(fù)功能詳解

    目前,各種App的社區(qū)或者用戶曬照片、發(fā)說說的地方,都提供了評論功能,為了更好地學(xué)習(xí),自己把這個(gè)功能實(shí)現(xiàn)了一下,下面這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)社交應(yīng)用中的評論與回復(fù)功能的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • 詳解Android啟動第一幀

    詳解Android啟動第一幀

    這篇文章我們就來介紹Android啟動第一幀,至于Android第一幀什么時(shí)候開始調(diào)度,具體內(nèi)容我們就來看下面文章內(nèi)容吧,感興趣得小伙伴可以和小編一起來學(xué)習(xí)奧
    2021-10-10
  • Android?XML數(shù)據(jù)解析要點(diǎn)介紹

    Android?XML數(shù)據(jù)解析要點(diǎn)介紹

    這篇文章主要為大家介紹了Android?XML數(shù)據(jù)解析要點(diǎn)介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Android 獲取藍(lán)牙Mac地址的正確方法

    Android 獲取藍(lán)牙Mac地址的正確方法

    android 從6.0開始,通過BluetoothAdapter.getDefaultAdapter().getAddress()獲取的地址是一個(gè)固定值02:00:00:00:00:00。下面給大家介紹Android 獲取藍(lán)牙Mac地址的正確方法,一起看看吧
    2017-12-12
  • Android統(tǒng)一依賴管理的三種方式總結(jié)

    Android統(tǒng)一依賴管理的三種方式總結(jié)

    為了項(xiàng)目的管理,依賴包的紡一管理是必要的,下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一依賴管理的三種方式,文中通過實(shí)例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • kotlin中object關(guān)鍵字的三種使用場景

    kotlin中object關(guān)鍵字的三種使用場景

    這篇文章主要給大家介紹了關(guān)于kotlin中object關(guān)鍵字的三種使用場景,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Android開發(fā)使用Message對象分發(fā)必備知識點(diǎn)詳解

    Android開發(fā)使用Message對象分發(fā)必備知識點(diǎn)詳解

    這篇文章主要為大家介紹了Android開發(fā)使用Message對象分發(fā)必備知識點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • android AsynTask處理返回?cái)?shù)據(jù)和AsynTask使用get,post請求

    android AsynTask處理返回?cái)?shù)據(jù)和AsynTask使用get,post請求

    本文主要介紹了android AsynTask處理返回?cái)?shù)據(jù)和AsynTask使用get,post請求方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • Android仿微信聯(lián)系人按字母排序

    Android仿微信聯(lián)系人按字母排序

    但凡涉及到聯(lián)系人界面,幾乎都是按照字母排序的,那么聯(lián)系人按字母排序是怎么實(shí)現(xiàn)的呢,下面小編就給大家詳解Android仿微信聯(lián)系人按字母排序,需要的朋友可以參考下
    2015-08-08

最新評論