點擊第一個按鈕,然後再第二個頁面中輸入文字,就會在第一個的EditText中出現在第二個Activity中輸入的文字,、
代碼一看就能明白startActivityForResult函數
第一個Activity
-
-
- import Android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
-
- public class DataBackActivity extends Activity {
- /** Called when the activity is first created. */
- private static final int REQUESTCODE=1;
- private Button button;
- private EditText editText;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button=(Button) findViewById(R.id.button1);
- editText=(EditText) findViewById(R.id.editText1);
- button.setOnClickListener(new MyButtonLIstener());
- }
- class MyButtonLIstener implements OnClickListener{
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setClass(DataBackActivity.this, To.class);
- startActivityForResult(intent, REQUESTCODE);
- }
- }
- /* (non-Javadoc)
- * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
- */
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- if (requestCode==REQUESTCODE) {
- switch (resultCode) {
- case RESULT_OK:
- String dateString=data.getExtras().getString("Da");
- Log.i("----------->", dateString);
- editText.setText(dateString);
- break;
-
- default:
- break;
- }
- }
-
-
- super.onActivityResult(requestCode, resultCode, data);
- }
-
- }
第二個Activity
- package rw.data;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
-
- public class To extends Activity {
- /** Called when the activity is first created. */
-
- private Button button;
- private EditText editText;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.to);
- button=(Button) findViewById(R.id.button1);
- editText=(EditText) findViewById(R.id.editText1);
- button.setOnClickListener(new MyButtonLIstener());
- }
- class MyButtonLIstener implements OnClickListener{
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.putExtra("Da", editText.getText().toString());
- To.this.setResult(RESULT_OK, intent);
- To.this.finish();
- }
- }
- }