如何从另一个 activity 中获取 EditText 的值?
How to take the EditText's value from one activity in another?
有两个活动。我想将在 EditText 字段 dat 中输入的内容从第一个屏幕带入第二个屏幕。我在第一个 activity:
中使用了这段代码
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 所在的地方。
接下来 activity:
Bundle extras = getIntent().getExtras();
newString= extras.getString("STRING_I_NEED");
有两个活动。我想将在 EditText 字段 dat 中输入的内容从第一个屏幕带入第二个屏幕。我在第一个 activity:
中使用了这段代码 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 所在的地方。
接下来 activity: Bundle extras = getIntent().getExtras(); newString= extras.getString("STRING_I_NEED");