【发布时间】:2018-03-01 07:44:27
【问题描述】:
我正在关注在两个活动之间传递数据的 android 教程,使用 startActivityForResult() 方法。该示例完美运行。但是,一旦我输入并传递了 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();
}
}
【问题讨论】:
-
然后将您之前的消息存储在某个数组或列表中
-
@sumit,感谢您的建议,您能否提供一个如何执行此操作的流程,因为我对 android 很陌生
标签: android android-intent startactivityforresult