如何从一个活动发送到另一个字符串?发送到、字符串

由网友(我六毛你六毛咱俩一块二)分享简介:所以,我有一个字符串的活性2 So i have a string in activity2String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s", lat, lng); 我想插入这个字符串转换成文...

所以,我有一个字符串的活性2

So i have a string in activity2

    String message = String.format(
"Current Location n Longitude: %1$s n Latitude: %2$s", lat, lng); 

我想插入这个字符串转换成文本字段的活动1。我该怎么办呢? 谢谢你在前进。

i want to insert this string into text field in activity1. how can i do that? Thank you in advance.

推荐答案

您可以使用意图,这是活动之间发送的消息。在意向,你可以把所有类型的数据,字符串,INT等。

You can use intents, which are messages sent between activities. In a intent you can put all sort of data, String, int, etc.

在你的情况下,在活性2 ,才去活动1 ,你将存储一个String这样的留言:

In your case, in activity2, before going to activity1, you will store a String message this way :

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

活动1 的onCreate(),即可获得字符串通过检索消息的捆绑(其中包含了所有调用活动发送的消息),并调用的getString()就可以了:

In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

然后就可以设置为文本中的的TextView

TextView txtView = (TextView) findViewById(R.id.your_resource_textview);    
txtView.setText(message);

希望这有助于!

Hope this helps !

阅读全文

相关推荐

最新文章