Android - 问题 使用意图在两个活动之间传递和保存数据



我在第一个活动中有一个TextView和一个Button,在我的第二个活动中有一个EditText和一个Button

在第二个活动中,我想将EditText值传递给第一个活动并在第一个活动中显示TextView,我还想在从第一个活动返回后保存给定的 edittext 值

public class ActivityA extends Activity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.tv1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gotoActivityB();
}
});
}
/*  private void gotoActivityB(){
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 0);  //0 is a request code
}*/
private void gotoActivityB()
{
Intent intent = new Intent(this,ActivityB.class);
intent.putExtra("value",textView.getText());
startActivityForResult(new Intent(this,ActivityB.class),101);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra("keyName");
// set text view with string
textView.setText(returnString);
}
}
}
}

第二活动

public class ActivityB extends Activity {
EditText edittext;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
edittext=(EditText)findViewById(R.id.edt);
Intent intent = getIntent();
String str = intent.getStringExtra("value"); // that means you have passed value from activity B to activity A, otherwise activity B is fresh launched.
if(str!=null)
{
edittext.setText(str);
}
button=(Button)findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get the text from the EditText
EditText editText = (EditText) findViewById(R.id.edt);
String stringToPassBack = editText.getText().toString();
// put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.putExtra("keyName", stringToPassBack);
setResult(RESULT_OK, intent);
finish();
}
});
}
}

ActivityA中,将gotoActivityB()方法更改为

private void gotoActivityB(){
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 0);  //0 is a request code
}

然后在ActivityA中重写onActivityResult方法

// This method is called when the second activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra("keyName");
// set text view with string
textView.setText(returnString);
}
}
}

在您的ActivityB中,单击按钮时编写以下代码

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get the text from the EditText
EditText editText = (EditText) findViewById(R.id.editText);
String stringToPassBack = editText.getText().toString();
// put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.putExtra("keyName", stringToPassBack);
setResult(RESULT_OK, intent);
finish();
}
});

还要从ActivityB中删除onDestroy()方法,这是不需要的。

在按钮单击的第二个活动中

使用这样的代码:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("KEY",value);
setResult(Activity.RESULT_OK,intent);
finish();
}
});

最新更新