一.新建一个空的工程
二.在主界面中添加一个按钮和一个TextView组件
三.新建一个空的activity,命名为TheAty,并添加一个按钮和一个EditText组件
四.重写TheAty的源代码文件中的onCreate函数
private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_the_aty); editText = (EditText)findViewById(R.id.editText); findViewById(R.id.bynSendBack).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.putExtra("data",editText.getText().toString()); setResult(1,i); finish(); } }); }
五.修改MainActivity.java中的onCreate函数
private TextView textView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, TheAty.class); startActivityForResult(i,0); } });
六.重写onAcitivyResult函数
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); textView.setText("The data for another activity is "+data.getStringExtra("data")); }
七.运行结果