我在activity2中有一个字符串
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s", lat, lng);
我想将这个字符串插入到activity1中的文本字段中。我该怎么做?
我在activity2中有一个字符串
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s", lat, lng);
我想将这个字符串插入到activity1中的文本字段中。我该怎么做?
你可以使用意图(intents),它们是在活动之间发送的消息。在意图中,你可以放置各种数据,例如字符串(String)、整数(int)等。
在你的情况下,在去往activity1
之前,在activity2
中以这种方式存储一个字符串消息:
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
在activity1
中的onCreate()
方法中,你可以通过检索包含由调用活动发送的所有消息的Bundle
并在其上调用getString()
来获取String
消息:Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
然后您可以在TextView
中设置文本:
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
private TextView editText1
,对吗?谢谢。 - Luch FilipIntent
不会“存储字符串”,它会将该字符串(或您传递的任何数据)传递给下一个活动。(2) 您不需要获取整个 Bundle
来获取通过 Intent
传递的值。您可以直接访问它们。 - Rarw你可以使用 Intent
在一种活动中发送数据到另一种活动。
Intent sendStuff = new Intent(this, TargetActivity.class);
sendStuff.putExtra(key, stringvalue);
startActivity(sendStuff);
接下来您可以通过获取意图并提取字符串附加信息,在第二个活动中检索此信息。请在onCreate()
方法中执行此操作。
Intent startingIntent = getIntent();
String whatYouSent = startingIntent.getStringExtra(key, value);
然后你只需在你的 TextView
上调用setText并使用该字符串即可。当我们谈论在活动之间传递数据时,有两种可能的情况。
假设有两个活动A和B,还有一个字符串X,并且您在Activity A中。
现在让我们看看这两种情况:
情况1:
字符串X在Activity A中,您想要在Activity B中获取它。
这很简单。
在Activity A中:
1)创建Intent
2)放入Extra值
3)启动startActivity
Intent i = new Intent(A.this, B.class);
i.putExtra("Your_KEY",X);
startActivity(i)
在Activity B中
在onCreate()
方法内,使用您存储X时使用的键(Your_KEY)检索字符串X。
Intent i = getIntent();
String s = i.getStringExtra("Your_KEY");
案例2:
如果你是Android开发新手,这个案例可能会有点棘手。因为你在Activity A中,前往Activity B,收集字符串(或数据),然后返回Activity A并检索收集到的字符串。让我们看看如何处理这种情况。
在Activity A中:
1)创建Intent
2)使用请求代码启动一个活动。
Intent i = new Intent(A.this, B.class);
startActivityForResult(i,your_req_code);
在B活动中
1)将字符串X放入意图中
2)设置结果
3)完成活动
Intent returnIntent = new Intent();
returnIntent .putString("KEY",X);
setResult(resCode,returnIntent); // for the first argument, you could set Activity.RESULT_OK or your custom rescode too
finish();
onActivityResult(int req_code, int res_code, Intent data)
{
if(req_code==your_req_code)
{
String X = data.getStringExtra("KEY")
}
}
你可能会想知道onActivityResult(int reqCode, resCode, Intent data)
中的reqCode和resCode是什么。
reqCode在你需要从哪个活动中获取结果时很有用。
比如说,你有两个按钮,一个按钮启动相机(你点击照片并在你的Activity中获得该图像的位图作为结果),另一个按钮启动GoogleMap(你将当前位置的坐标作为结果返回)。所以为了区分两个活动的结果,你会用不同的请求代码启动CameraActivty和MapActivity。
resCode:在需要区分请求活动中的结果“如何”返回时非常有用。
例如:你启动相机活动。当相机活动开始时,你可以拍照或者只是通过返回按钮回到请求活动而没有拍照。因此,在这两种情况下,你的相机活动会使用不同的resCode ACTIVITY.RESULT_OK和ACTIVITY.RESULT_CANCEL发送结果。
相关链接
假设您的MainActivity中有一个EditText et1,您想将其传递到SecondActivity
String s=et1.getText().toString();
Bundle basket= new Bundle();
basket.putString("abc", s);
Intent a=new Intent(MainActivity.this,SecondActivity.class);
a.putExtras(basket);
startActivity(a);
现在在第二个活动中,假设您想将从EditText et1传递的字符串放入SecondActivity的TextView txt1中。
Bundle gt=getIntent().getExtras();
str=gt.getString("abc");
txt1.setText(str);
Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
public void visitactivity1()
{
Intent i = new Intent(this, activity1.class);
i.putExtra("key", message);
startActivity(i);
}
创建完这个函数后,你需要在activity2的onCreate()函数中调用它:
visitactivity1();
Bundle b = getIntent().getExtras();
String message = b.getString("key", ""); // the blank String in the second parameter is the default value of this variable. In case the value from previous activity fails to be obtained, the app won't crash: instead, it'll go with the default value of an empty string
我在做这个,但我的输出很奇怪,这是第一个活动
up = findViewById(R.id.button);
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, updatestudents.class);
intent.putExtra("updating",updating);
startActivity(intent);
}
});
这是第二个活动
Bundle extras = getIntent().getExtras();
if (extras != null) {
Current_Value = getIntent().getStringExtra("updating");
}
u = findViewById(R.id.text);
u.setText("updating " + Current_Value);
这里我在第二个活动中检索字符串
这是我的输出输入图像说明
在需要大量与Activity交互的情况下,您可以使用GNLauncher,这是我编写的一种实用程序库的一部分。通过该库,只需在Activity对象上调用一个带有所需参数的函数,就几乎可以像简单地调用函数一样进行操作。https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher
意图在 Android 框架中传递数据非常有用。您可以与自己的活动
甚至其他进程进行通信。请查看开发人员指南,如果您有具体问题(这是一个很大的前提),请回来。
Kotlin
的人,请这样做:例如,
// * The Method I Mentioned Above
private fun parseTheValue(@NonNull valueYouWantToParse: String)
{
val intent = Intent(this, AnotherActivity::class.java)
intent.putExtra("value", valueYouWantToParse)
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent)
this.finish()
}
然后只需调用parseTheValue("您想解析的字符串")
e.g,
val theValue: String
parseTheValue(theValue)
然后在另一个活动中,
val value: Bundle = intent.extras!!
// * enter the `name` from the `@param`
val str: String = value.getString("value").toString()
// * For testing
println(str)
希望这能帮到你,编程愉快!
~ John Melody 添加的 Kotlin 代码 ~