[英]How to pass a String array value from one activity to another Activity in android?
[英]How to pass a value from one Activity to another in Android?
我已經用AutuCompleteTextView [ACTV]和按鈕創建了一個Activity。 我在ACTV中輸入一些文字,然后按按鈕。 我按下按鈕后,我希望活動轉到另一個活動。 在第二個活動中,我只想將在ACTV(第一個活動)中輸入的文本顯示為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(View v)”為按鈕創建了事件處理程序類。
您可以使用Bundle在Android中執行相同的操作
創建意圖:
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“stuff”, getrec);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
現在在第二個活動中,從捆綁包中檢索數據:
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String stuff = bundle.getString(“stuff”);
將數據從一個活動傳遞到另一個活動的標准方式:
如果要將大量數據從一個活動發送到另一個活動,則可以將數據放入捆綁包,然后使用putExtra()
方法傳遞數據。
//Create the `intent`
Intent i = new Intent(this, ActivityTwo.class);
String one="xxxxxxxxxxxxxxx";
String two="xxxxxxxxxxxxxxxxxxxxx";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“ONE”, one);
bundle.putString(“TWO”, two);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
否則,您可以直接使用putExtra()
來發送數據,而直接使用getExtra()
來獲取數據。
Intent i=new Intent(this, ActivityTwo.class);
i.putExtra("One",one);
i.putExtra("Two",two);
startActivity(i);
這很簡單,請使用Intent.putExtra將數據傳遞給您開始的活動。 然后使用Bundle.getExtra進行檢索。
已經有很多這樣的問題https://stackoverflow.com/search?q=How+to+pass+a+value+from+one+Activity+to+other+in+Android,請確保下次再次使用搜索。
以這種方式實施
String i="hi";
Intent i = new Intent(this, ActivityTwo.class);
//Create the bundle
Bundle b = new Bundle();
//Add your data to bundle
b.putString(“stuff”, i);
i.putExtras(b);
startActivity(i);
開始是第二個activity
,這里面的class
地利用捆綁值使用此代碼
Bundle bundle = getIntent().getExtras();
String text= bundle.getString("stuff");
很簡單,如果您要從A到B傳遞字符串X。
A-> B
在活動A中
1)創建意圖
2)使用意圖的putExtra方法將數據放入意圖
3)開始活動
Intent i = new Intent(A.this, B.class);
i.putExtra("MY_kEY",X);
在活動B中
里面的onCreate方法
1)獲取意圖對象
2)使用鍵(MY_KEY)獲取儲值
Intent intent = getIntent();
String result = intent.getStringExtra("MY_KEY");
這是從A到B發送數據的標准方式。您可以發送任何數據類型,它可以是int,boolean,ArrayList,String []。 根據您存儲在“活動”中作為鍵的數據類型,值對的檢索方法可能會有所不同,例如,如果要傳遞int值,則將調用
intent.getIntExtra("KEY");
您甚至可以發送Class對象,但是為此,您必須使您的類對象實現Serializable或Parceable接口。
您可以跨大小發送多少數據。 如果數據超過一定數量,則可能會收到TransactionTooLargeException。 假設您嘗試在整個活動中發送位圖,並且如果大小超出某些數據大小,則可能會看到此異常。
在第一個活動中:
Intent i=new Intent(getApplicationContext,secondActivity.class);
i.putExtra("key",value);
startActivity(i);
在SecondActivity中:
String value=getIntent.getStringExtra("Key");
示例Kotlin代碼如下:-
第1頁
val i = Intent(this, Page2::class.java)
val getrec = list[position].promotion_id
val bundle = Bundle()
bundle.putString("stuff", getrec)
i.putExtras(bundle)
startActivity(i)
第2頁
var bundle = getIntent().getExtras()
var stuff = bundle.getString("stuff")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.