使用 startActivityForResult() 方法在 Android 个活动之间传递数据并保留这些数据
Using startActivityForResult() method to pass data between Android activities and retain those data
我正在学习 android 在两个活动之间传递数据的教程
开始ActivityForResult() 方法。该示例完美运行。但是一旦我为 Main Activity 输入并传递一个值并再次尝试发送一个新值,之前输入的值将被替换。我想要的是保留我输入并传递给 Main Activity 的所有值而不消失。请任何人都可以指导我完成。感谢您的帮助。
main.Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/textViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Message"
android:textColor="#031241"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:onClick="getMessage"
android:text="Get Message" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get The reference of The textView
textViewMessage = (TextView) findViewById(R.id.textViewMessage);
}
// Method to handle the Click Event on GetMessage Button
public void getMessage(View V) {
// Create The Intent and Start The Activity to get The message
Intent intentGetMessage = new Intent(this, SecondActivity.class);
startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (requestCode == 2) {
if (null != data) {
// fetch the message String
String message = data.getStringExtra("MESSAGE");
// Set the message string in textView
textViewMessage.setText("Message from second Activity: " + message);
}
}
}
}
second.Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:hint="Enter The Message" />
<Button
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Submit Message"
android:onClick="submitMessage" />
</LinearLayout>
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Get the reference of Edit Text
editTextMessage=(EditText)findViewById(R.id.editTextMessage);
}
public void submitMessage(View V)
{
// get the Entered message
String message=editTextMessage.getText().toString();
Intent intentMessage=new Intent();
// put the message in Intent
intentMessage.putExtra("MESSAGE",message);
// Set The Result in Intent
setResult(2,intentMessage);
// finish The activity
finish();
}
}
使用这个:
String[] previousValue = new String[10]; // length of your array
int i = 0;
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (requestCode == 2) {
if (null != data) {
// fetch the message String
String message = data.getStringExtra("MESSAGE");
previousValue[i] = message;
i++;
// Set the message string in textView
textViewMessage.setText("Message from second Activity: " + message);
}
}
}
//然后你想在哪里使用以前的值只是遍历数组。
for(int i = 0; previousValue.length(); i++){
//displaying all the previous values
Log.e("someTag","value : "+previousValue[i]);
}
我正在学习 android 在两个活动之间传递数据的教程 开始ActivityForResult() 方法。该示例完美运行。但是一旦我为 Main Activity 输入并传递一个值并再次尝试发送一个新值,之前输入的值将被替换。我想要的是保留我输入并传递给 Main Activity 的所有值而不消失。请任何人都可以指导我完成。感谢您的帮助。
main.Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/textViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Message"
android:textColor="#031241"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:onClick="getMessage"
android:text="Get Message" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get The reference of The textView
textViewMessage = (TextView) findViewById(R.id.textViewMessage);
}
// Method to handle the Click Event on GetMessage Button
public void getMessage(View V) {
// Create The Intent and Start The Activity to get The message
Intent intentGetMessage = new Intent(this, SecondActivity.class);
startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (requestCode == 2) {
if (null != data) {
// fetch the message String
String message = data.getStringExtra("MESSAGE");
// Set the message string in textView
textViewMessage.setText("Message from second Activity: " + message);
}
}
}
}
second.Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:hint="Enter The Message" />
<Button
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Submit Message"
android:onClick="submitMessage" />
</LinearLayout>
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Get the reference of Edit Text
editTextMessage=(EditText)findViewById(R.id.editTextMessage);
}
public void submitMessage(View V)
{
// get the Entered message
String message=editTextMessage.getText().toString();
Intent intentMessage=new Intent();
// put the message in Intent
intentMessage.putExtra("MESSAGE",message);
// Set The Result in Intent
setResult(2,intentMessage);
// finish The activity
finish();
}
}
使用这个:
String[] previousValue = new String[10]; // length of your array
int i = 0;
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (requestCode == 2) {
if (null != data) {
// fetch the message String
String message = data.getStringExtra("MESSAGE");
previousValue[i] = message;
i++;
// Set the message string in textView
textViewMessage.setText("Message from second Activity: " + message);
}
}
}
//然后你想在哪里使用以前的值只是遍历数组。
for(int i = 0; previousValue.length(); i++){
//displaying all the previous values
Log.e("someTag","value : "+previousValue[i]);
}