我在activity2中有一个字符串
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s", lat, lng);
我想将此字符串插入到 activity1 的文本字段中。我该怎么做?
最佳答案
您可以使用 Intent ,即在 Activity 之间发送的消息。在一个 Intent 中,您可以放置所有类型的数据、字符串、整数等。
在您的情况下,在 activity2
中,在转到 activity1
之前,您将以这种方式存储字符串消息:
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
在activity1
中,在onCreate()
中,可以通过检索Bundle
(其中包含调用 Activity 发送的所有消息)并在其上调用 getString()
:
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
然后可以在TextView
中设置文字:
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
希望这会有所帮助!
关于android - 如何将字符串从一项 Activity 发送到另一项 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146614/