如何在Android中將一個值從一個活動傳遞到另一個活動?我用AutuCompleteTextView[ACTV]和按鈕創(chuàng)建了一個活動。我在ACTV中輸入一些文本,然后按下按鈕。按下按鈕后,我希望活動轉(zhuǎn)到另一個活動。在第二個活動中,我只想將在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”。請假定我已經(jīng)使用“onClick(Viewv)”為按鈕創(chuàng)建了事件處理程序類。
3 回答

慕俠2389804
TA貢獻1719條經(jīng)驗 獲得超6個贊
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”);

慕碼人8056858
TA貢獻1803條經(jīng)驗 獲得超6個贊
將數(shù)據(jù)從一項活動傳遞到另一項活動的標(biāo)準(zhǔn)方法:
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);

鴻蒙傳說
TA貢獻1865條經(jīng)驗 獲得超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");
- 3 回答
- 0 關(guān)注
- 974 瀏覽
添加回答
舉報
0/150
提交
取消