首先建立两个activity界面
Activity1如下
public class MainActivity extends AppCompatActivity {
private Button bt;
private TextView t1;
private TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button)findViewById(R.id.bt1);
t1=(TextView)findViewById(R.id.tv1);
t2=(TextView)findViewById(R.id.tv2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class); //从MainActivity跳转到Main2Activity界面
startActivityForResult(intent,1); //返回值的Intent开始方法,后边“1”表示其请求码,可以随便起
//startActivity(intent) 直接跳转,不返回值
}
});
}
@Override
//配合intent方法一起使用
protected void onActivityResult(int requestCode,int resultCode,Intent date){
super.onActivityResult(requestCode,resultCode,date);
if (date!=null){
String s1=date.getStringExtra("result1"); //得到intent对象输入的字符串
t1.setText("QQ账号"+s1);//赋值给Textview 显示出来
String s2=date.getStringExtra("result2");
t2.setText("QQ密码"+s2);
}
}
}
Main2Activity界面
public class Main2Activity extends AppCompatActivity {
private EditText e1;
private EditText e2;
private Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt1=(Button)findViewById(R.id.bt1);
e1=(EditText)findViewById(R.id.et1);
e2=(EditText)findViewById(R.id.et2);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=getIntent();//跳转 后得到intnent对象
String sr1=e1.getText().toString(); //得到EditTextview的输入值,转为string字符串
String sr2=e2.getText().toString();
intent.putExtra("result1",sr1); //把得到的字符串输入到intent对象中,“result”用于代表它的代号,可以随便定义。
intent.putExtra("result2",sr2);
setResult(1,intent); //返回intent 对象的输入值,“1”为其结果码
//setReult(inttent)返回跳转
finish();//结束
}
});
}
}