Click the first button, and then enter text on the second page. The text entered in the second Activity appears in the first EditText ,,
The code can understand the startActivityForResult function at a glance.
First 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 );
}
} Www.2cto.com
/* (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 );
}
}
Second 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. );
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 ();
}
}
}
From: Viagra ~ YZ