如何从一个活动发送到另一个字符串? [英] How to send string from one activity to another?

查看:153
本文介绍了如何从一个活动发送到另一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个字符串的活性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 !

这篇关于如何从一个活动发送到另一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆