![](/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.