![](/img/trans.png)
[英]How to get an EditText value to go from one activity to another as an int
[英]How to take the EditText's value from one activity in another?
有兩個活動。 我想將在EditText字段dat中輸入的內容從第一個屏幕帶入第二個屏幕。 我在第一個活動中使用了以下代碼:
Intent i = new Intent(this.getApplicationContext(),MainActivity.class);
i.putExtra((inputuserName.getText()).toString(), true);
startActivity(i);
如何在第二個中獲取值? 謝謝!
您好,為什么要嘗試發送布爾類型true或false? 您只需要發送字符串值。 看到,
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("STRING_I_NEED", inputuserName.getText()).toString());
startActivity(i);
然后,要檢索該值,請嘗試如下操作:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
謝謝
您必須在要傳遞的值上附加一條消息:i.putExtra(“ STRING_I_NEED”,strName); 因此,您要將inputuserName.getText())。toString()放在strName所在的位置。
在下一個活動中:捆綁包= getIntent()。getExtras(); newString = extras.getString(“ STRING_I_NEED”);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.