android - 如何将字符串从一项 Activity 发送到另一项 Activity ?

标签 android android-activity

我在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/

相关文章:

java - 未经检查的方法调用作为原始类型的成员

android - 在后退按钮上退出 Android 应用程序

android - 什么传感器可以用来检测直立时的旋转?

android - 如何在不点击的情况下在谷歌地图可组合标记上显示标题和 fragment ?

作为额外传递的 Android 应用程序 : Runtime error when I try to get the Parcelable,

android - 没有网络连接时如何调用新布局?

android - 从源代码 : make: no rule to make target *. 构建 CyanogenMod,*.so 需要

Android:如何使 ListView 中的项目打开以进行比较。 Activity/Intent

java - Android: onActivityResult() 本地数据丢失

android - 如何从 AsyncTask 启动 Intent