我的第一个活动中有TextView
和Button
,第二个活动中有EditText
和Button
。
在第二个活动中,我想将EditText
值传递给第一个活动并在第一个活动中显示TextView
,我还想在从第一个活动返回后保存给定的edittext值< / p>
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);
}
}
}
}
Secondactivity
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();
}
});
}
}
答案 0 :(得分:1)
在第二个活动按钮上单击
使用这样的代码:
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();
}
});
答案 1 :(得分:1)
在ActivityA
中,将gotoActivityB()
方法更改为
private void gotoActivityB(){
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 0); //0 is a request code
}
然后覆盖onActivityResult
ActivityA
方法
// 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();
}
});
也会从onDestroy()
中删除ActivityB
方法,但不需要。{/ p>