在Android中怎么實(shí)現(xiàn)與Activity的數(shù)據(jù)傳遞?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
在臺(tái)州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),成都營(yíng)銷網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),臺(tái)州網(wǎng)站建設(shè)費(fèi)用合理。使用Inten的putExtra傳遞
第一個(gè)Activity中
//創(chuàng)建意圖對(duì)象 Intent intent = new Intent(this,TwoActivity.class); //設(shè)置傳遞鍵值對(duì) intent.putExtra("data",str); //激活意圖 startActivity(intent);
第二個(gè)Activity中
// 獲取意圖對(duì)象 Intent intent = getIntent(); //獲取傳遞的值 String str = intent.getStringExtra("data"); //設(shè)置值 tv.setText(str);
使用Intention的Bundle傳遞
第一個(gè)Activity中
//創(chuàng)建意圖對(duì)象 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)建意圖對(duì)象 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", ""));
使用序列化對(duì)象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對(duì)象中 bean.setName("啦啦"); bean.setSex("男"); intent.putExtra("key", bean); startActivity(intent);
第二個(gè)Activity
Intent intent = getIntent(); //反序列化數(shù)據(jù)對(duì)象 Serializable se = intent.getSerializableExtra("key"); if(se instanceof DataBean){ //獲取到攜帶數(shù)據(jù)的DataBean對(duì)象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);
看完上述內(nèi)容,你們掌握在Android中怎么實(shí)現(xiàn)與Activity的數(shù)據(jù)傳遞的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站題目:在Android中怎么實(shí)現(xiàn)與Activity的數(shù)據(jù)傳遞-創(chuàng)新互聯(lián)
本文URL:http://aaarwkj.com/article10/jcjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、手機(jī)網(wǎng)站建設(shè)、軟件開發(fā)、響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容