亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Android中將一個值從一個活動傳遞到另一個活動?

如何在Android中將一個值從一個活動傳遞到另一個活動?

函數式編程 2019-07-20 13:20:40
如何在Android中將一個值從一個活動傳遞到另一個活動?我用AutuCompleteTextView[ACTV]和按鈕創建了一個活動。我在ACTV中輸入一些文本,然后按下按鈕。按下按鈕后,我希望活動轉到另一個活動。在第二個活動中,我只想將在ACTV(第一個Actvity)中輸入的文本顯示為TextView。我知道如何開始第二項活動,具體如下:Intent i = new Intent(this, ActivityTwo.class);startActivity(i);我對此進行了編碼,以獲得從ACTV輸入的文本。AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);CharSequence getrec=textView.getText();這里的問題是如何將“getrec”(在我按下按鈕后)從第一個活動傳遞到第二個活動。后來在第二次活動中收到了“getrec”。請假定我已經使用“onClick(Viewv)”為按鈕創建了事件處理程序類。
查看完整描述

3 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

您可以在Android中使用Bundle進行同樣的操作。

創建意圖:

Intent i = new Intent(this, ActivityTwo.class);AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);String getrec=textView.getText().toString();//Create the bundleBundle bundle = new Bundle();//Add your data to bundlebundle.putString(“stuff”, getrec);//Add the bundle to the intenti.putExtras(bundle);//Fire that second activitystartActivity(i);

現在,在第二個活動中,從包中檢索數據:

//Get the bundleBundle bundle = getIntent().getExtras();//Extract the data…String stuff = bundle.getString(“stuff”);


查看完整回答
反對 回復 2019-07-20
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

將數據從一項活動傳遞到另一項活動的標準方法:

如果要將大量數據從一個活動發送到另一個活動,則可以將數據放入一個包中,然后使用putExtra()方法。

//Create the `intent`
 Intent i = new Intent(this, ActivityTwo.class);String one="xxxxxxxxxxxxxxx";String two="xxxxxxxxxxxxxxxxxxxxx";//Create the bundleBundle bundle = new Bundle();//Add your data to bundlebundle.putString(“ONE”, one);bundle.putString(“TWO”, two);  //Add the bundle to the intenti.putExtras(bundle);//Fire that second activitystartActivity(i);

否則您可以使用putExtra()直接發送數據和getExtra()來獲取數據。

Intent i=new Intent(this, ActivityTwo.class);i.putExtra("One",one);i.putExtra("Two",two);startActivity(i);


查看完整回答
反對 回復 2019-07-20
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

以這種方式實施

String i="hi";Intent i = new Intent(this, ActivityTwo.class);//Create the bundleBundle b = new Bundle();//Add your data to bundleb.putString(“stuff”, i);i.putExtras(b);startActivity(i);

開始那第二次activity,在這里面class若要使用綁定值,請使用以下代碼

Bundle bundle = getIntent().getExtras();String text= bundle.getString("stuff");


查看完整回答
反對 回復 2019-07-20
  • 3 回答
  • 0 關注
  • 811 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號