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

时间:2023-01-15 20:45:52

So i have a string in activity2

所以我在activity2中有一个字符串

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

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

我想将此字符串插入activity1中的文本字段。我怎样才能做到这一点?先谢谢你。

8 个解决方案

#1


80  

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

您可以使用意图,这是在活动之间发送的消息。在一个intent中你可以放置所有类型的数据,String,int等。

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

在您的情况下,在activity2中,在转到activity1之前,您将以这种方式存储String消息:

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

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 :

在activity1中,在onCreate()中,您可以通过检索Bundle(包含调用活动发送的所有消息)来获取String消息,并在其上调用getString():

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

Then you can set the text in the TextView:

然后,您可以在TextView中设置文本:

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

Hope this helps !

希望这可以帮助 !

#2


13  

You can send data from one actvity to another with an Intent

您可以使用Intent将数据从一个活动发送到另一个活动

Intent sendStuff = new Intent(this, TargetActivity.class);
sendStuff.putExtra(key, stringvalue);
startActivity(sendStuff);

You then can retrieve this information in the second activity by getting the intent and extracting the string extra. Do this in your onCreate() method.

然后,您可以通过获取意图并提取额外的字符串来在第二个活动中检索此信息。在你的onCreate()方法中执行此操作。

Intent startingIntent = getIntent();
String whatYouSent = startingIntent.getStringExtra(key, value);

Then all you have to do is call setText on your TextView and use that string.

然后,您所要做的就是在TextView上调用setText并使用该字符串。

#3


4  

Say there is EditText et1 in ur MainActivity and u wanna pass this to SecondActivity

假设你的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);

now in Second Activity, say u wanna put the string passed from EditText et1 to TextView txt1 of SecondActivity

现在在Second Activity中,假设您想将从EditText et1传递的字符串放到SecondActivity的TextView txt1中

Bundle gt=getIntent().getExtras();
str=gt.getString("abc");
txt1.setText(str);

#4


3  

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

In activity2, 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 :

在activity2中,在onCreate()中,您可以通过检索Bundle(包含调用活动发送的所有消息)来获取String消息,并在其上调用getString():

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

#5


2  

TWO CASES

There are two situation possible when we talk about passing data between activities.

当我们讨论在活动之间传递数据时,有两种情况可能发生。

Let's say there are two activities A and B and there is a String X. and you are in Activity A.

假设有两个活动A和B,并且有一个字符串X.你在活动A.

Now two cases

现在有两个案例

1) A->B
2) A<-B

1)A-> B 2)A <-B

CASE 1): String X is in A and you want to get it in Activity B.

情况1):字符串X在A中,您希望在活动B中获取它。

It is very straightforward.

这非常简单。

In Activity A.

在活动A.

1) Create Intent
2) Put Extra value
3) startActivity

1)创建意图2)设置额外值3)startActivity

Intent i = new Intent(A.this, B.class);
i.putExtra("Your_KEY",X);
startActivity(i)

In Activity B

在活动B中

Inside onCreate method retrieve string X using key which you used while storing X (Your_KEY).

在onCreate方法内部使用您在存储X(Your_KEY)时使用的密钥检索字符串X.

Intent i = getIntent();
String s = i.getStringExtra("Your_KEY");

Case 2)
This case is little tricky if u are new in android development.
Bacause you are in activity A, you move to Activity B, collect the string, move back to Activity A and retrieve the collected String or data. Lets see how to deal this situation.

案例2)如果你是Android开发的新手,这个案子有点棘手。因为你在活动A,你移动到活动B,收集字符串,回到活动A并检索收集的字符串或数据。让我们看看如何处理这种情况。

In Activity A
1) Create Intent
2) start activity with a request code.

在活动A中1)创建意图2)使用请求代码启动活动。

Intent i = new Intent(A.this, B.class);
startActivityForResult(i,your_req_code);

In Activity B
1) Put string X in intent
2) Set result
3) Finish activity

在活动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();

Again in Activity A
1) Override onActivityResult method

再次在活动A中1)覆盖onActivityResult方法

onActicvityResult(int req_code, int res_code, Intent data)
{
       if(req_code==your_req_code)
       {
          String X = data.getStringExtra("KEY")
       }
}

Further understanding of Case 2

You might wonder what is the reqCode, resCode in the onActicvityResult(int reqCode, resCode, Intent data)

您可能想知道onActicvityResult中的reqCode,resCode是什么(int reqCode,resCode,Intent数据)

reqCode is useful when you have to identify from which activity you are getting result .

当您必须确定从哪个活动获得结果时,reqCode非常有用。

Let's say you have two buttons, one button starts Camera (you click a photo and get the bitmap of that image in your Activity as a result), another button starts GoogleMap( you get back the current coordinates of your location as a result). So to distinguish between the results of both activities you start CameraActivty and MapActivity with different request codes.

假设您有两个按钮,一个按钮启动摄像头(您单击一张照片并在活动中获取该图像的位图),另一个按钮启动GoogleMap(您将返回您所在位置的当前坐标)。因此,为了区分这两个活动的结果,您可以使用不同的请求代码启动CameraActivty和MapActivity。

resCode: is useful when you have to distinguish between how results are coming back to requesting activity.

resCode:在必须区分结果返回请求活动时非常有用。

For eg: You start Camera Activity. When the camera activity starts, you could either take a photo or just move back to requesting activity without taking a photo with back button press. So in these two situation your camera activity sends result with different resCode ACTIVITY.RESULT_OK and ACTIVITY.RESULT_CANCEL respectively.

例如:您启动相机活动。当相机活动开始时,您可以拍摄照片或只是返回请求活动而无需按后退按钮拍照。因此,在这两种情况下,您的相机活动会分别使用不同的resCode ACTIVITY.RESULT_OK和ACTIVITY.RESULT_CANCEL发送结果。

Relevant Links

Read more on Getting result

详细了解获得结果

#6


0  

Intents are intense.

意图很激烈。

Intents are useful for passing data around the android framework. You can communicate with your own Activities and even other processes. Check the developer guide and if you have specific questions (it's a lot to digest up front) come back.

Intent对于在android框架周围传递数据很有用。您可以与自己的活动甚至其他流程进行通信。查看开发者指南,如果您有特定问题(预先消化很多),请回来。

#7


0  

You can use the GNLauncher, which is part of a utility library I wrote in cases where a lot of interaction with the Activity is required. With the library, it is almost as simple as calling a function on the Activity object with the required parameters. https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher

您可以使用GNLauncher,它是我在需要与Activity进行大量交互的情况下编写的实用程序库的一部分。使用库,它几乎就像使用所需参数调用Activity对象上的函数一样简单。 https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher

#8


0  

In order to insert the text from activity2 to activity1, you first need to create a visit function in activity2.

要将activity2中的文本插入到activity1,首先需要在activity2中创建一个visit函数。

public void visitactivity1()
{
    Intent i = new Intent(this, activity1.class);
    i.putExtra("key", message);
    startActivity(i);
}

After creating this function, you need to call it from your onCreate() function of activity2:

创建此函数后,需要从activity2的onCreate()函数调用它:

visitactivity1();

Next, go on to the activity1 Java file. In its onCreate() function, create a Bundle object, fetch the earlier message via its key through this object, and store it in a String.

接下来,转到activity1 Java文件。在其onCreate()函数中,创建一个Bundle对象,通过该对象通过其键获取先前的消息,并将其存储在String中。

    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

Now put this element in a TextView or EditText, or whichever layout element you prefer using the setText() function.

现在使用setText()函数将此元素放在TextView或EditText中,或者您喜欢的任何布局元素中。

#1


80  

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

您可以使用意图,这是在活动之间发送的消息。在一个intent中你可以放置所有类型的数据,String,int等。

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

在您的情况下,在activity2中,在转到activity1之前,您将以这种方式存储String消息:

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

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 :

在activity1中,在onCreate()中,您可以通过检索Bundle(包含调用活动发送的所有消息)来获取String消息,并在其上调用getString():

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

Then you can set the text in the TextView:

然后,您可以在TextView中设置文本:

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

Hope this helps !

希望这可以帮助 !

#2


13  

You can send data from one actvity to another with an Intent

您可以使用Intent将数据从一个活动发送到另一个活动

Intent sendStuff = new Intent(this, TargetActivity.class);
sendStuff.putExtra(key, stringvalue);
startActivity(sendStuff);

You then can retrieve this information in the second activity by getting the intent and extracting the string extra. Do this in your onCreate() method.

然后,您可以通过获取意图并提取额外的字符串来在第二个活动中检索此信息。在你的onCreate()方法中执行此操作。

Intent startingIntent = getIntent();
String whatYouSent = startingIntent.getStringExtra(key, value);

Then all you have to do is call setText on your TextView and use that string.

然后,您所要做的就是在TextView上调用setText并使用该字符串。

#3


4  

Say there is EditText et1 in ur MainActivity and u wanna pass this to SecondActivity

假设你的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);

now in Second Activity, say u wanna put the string passed from EditText et1 to TextView txt1 of SecondActivity

现在在Second Activity中,假设您想将从EditText et1传递的字符串放到SecondActivity的TextView txt1中

Bundle gt=getIntent().getExtras();
str=gt.getString("abc");
txt1.setText(str);

#4


3  

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

In activity2, 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 :

在activity2中,在onCreate()中,您可以通过检索Bundle(包含调用活动发送的所有消息)来获取String消息,并在其上调用getString():

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

#5


2  

TWO CASES

There are two situation possible when we talk about passing data between activities.

当我们讨论在活动之间传递数据时,有两种情况可能发生。

Let's say there are two activities A and B and there is a String X. and you are in Activity A.

假设有两个活动A和B,并且有一个字符串X.你在活动A.

Now two cases

现在有两个案例

1) A->B
2) A<-B

1)A-> B 2)A <-B

CASE 1): String X is in A and you want to get it in Activity B.

情况1):字符串X在A中,您希望在活动B中获取它。

It is very straightforward.

这非常简单。

In Activity A.

在活动A.

1) Create Intent
2) Put Extra value
3) startActivity

1)创建意图2)设置额外值3)startActivity

Intent i = new Intent(A.this, B.class);
i.putExtra("Your_KEY",X);
startActivity(i)

In Activity B

在活动B中

Inside onCreate method retrieve string X using key which you used while storing X (Your_KEY).

在onCreate方法内部使用您在存储X(Your_KEY)时使用的密钥检索字符串X.

Intent i = getIntent();
String s = i.getStringExtra("Your_KEY");

Case 2)
This case is little tricky if u are new in android development.
Bacause you are in activity A, you move to Activity B, collect the string, move back to Activity A and retrieve the collected String or data. Lets see how to deal this situation.

案例2)如果你是Android开发的新手,这个案子有点棘手。因为你在活动A,你移动到活动B,收集字符串,回到活动A并检索收集的字符串或数据。让我们看看如何处理这种情况。

In Activity A
1) Create Intent
2) start activity with a request code.

在活动A中1)创建意图2)使用请求代码启动活动。

Intent i = new Intent(A.this, B.class);
startActivityForResult(i,your_req_code);

In Activity B
1) Put string X in intent
2) Set result
3) Finish activity

在活动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();

Again in Activity A
1) Override onActivityResult method

再次在活动A中1)覆盖onActivityResult方法

onActicvityResult(int req_code, int res_code, Intent data)
{
       if(req_code==your_req_code)
       {
          String X = data.getStringExtra("KEY")
       }
}

Further understanding of Case 2

You might wonder what is the reqCode, resCode in the onActicvityResult(int reqCode, resCode, Intent data)

您可能想知道onActicvityResult中的reqCode,resCode是什么(int reqCode,resCode,Intent数据)

reqCode is useful when you have to identify from which activity you are getting result .

当您必须确定从哪个活动获得结果时,reqCode非常有用。

Let's say you have two buttons, one button starts Camera (you click a photo and get the bitmap of that image in your Activity as a result), another button starts GoogleMap( you get back the current coordinates of your location as a result). So to distinguish between the results of both activities you start CameraActivty and MapActivity with different request codes.

假设您有两个按钮,一个按钮启动摄像头(您单击一张照片并在活动中获取该图像的位图),另一个按钮启动GoogleMap(您将返回您所在位置的当前坐标)。因此,为了区分这两个活动的结果,您可以使用不同的请求代码启动CameraActivty和MapActivity。

resCode: is useful when you have to distinguish between how results are coming back to requesting activity.

resCode:在必须区分结果返回请求活动时非常有用。

For eg: You start Camera Activity. When the camera activity starts, you could either take a photo or just move back to requesting activity without taking a photo with back button press. So in these two situation your camera activity sends result with different resCode ACTIVITY.RESULT_OK and ACTIVITY.RESULT_CANCEL respectively.

例如:您启动相机活动。当相机活动开始时,您可以拍摄照片或只是返回请求活动而无需按后退按钮拍照。因此,在这两种情况下,您的相机活动会分别使用不同的resCode ACTIVITY.RESULT_OK和ACTIVITY.RESULT_CANCEL发送结果。

Relevant Links

Read more on Getting result

详细了解获得结果

#6


0  

Intents are intense.

意图很激烈。

Intents are useful for passing data around the android framework. You can communicate with your own Activities and even other processes. Check the developer guide and if you have specific questions (it's a lot to digest up front) come back.

Intent对于在android框架周围传递数据很有用。您可以与自己的活动甚至其他流程进行通信。查看开发者指南,如果您有特定问题(预先消化很多),请回来。

#7


0  

You can use the GNLauncher, which is part of a utility library I wrote in cases where a lot of interaction with the Activity is required. With the library, it is almost as simple as calling a function on the Activity object with the required parameters. https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher

您可以使用GNLauncher,它是我在需要与Activity进行大量交互的情况下编写的实用程序库的一部分。使用库,它几乎就像使用所需参数调用Activity对象上的函数一样简单。 https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher

#8


0  

In order to insert the text from activity2 to activity1, you first need to create a visit function in activity2.

要将activity2中的文本插入到activity1,首先需要在activity2中创建一个visit函数。

public void visitactivity1()
{
    Intent i = new Intent(this, activity1.class);
    i.putExtra("key", message);
    startActivity(i);
}

After creating this function, you need to call it from your onCreate() function of activity2:

创建此函数后,需要从activity2的onCreate()函数调用它:

visitactivity1();

Next, go on to the activity1 Java file. In its onCreate() function, create a Bundle object, fetch the earlier message via its key through this object, and store it in a String.

接下来,转到activity1 Java文件。在其onCreate()函数中,创建一个Bundle对象,通过该对象通过其键获取先前的消息,并将其存储在String中。

    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

Now put this element in a TextView or EditText, or whichever layout element you prefer using the setText() function.

现在使用setText()函数将此元素放在TextView或EditText中,或者您喜欢的任何布局元素中。