The main interface mainactivity implements the Onclicklistener interface
public class Mainactivity extends Activity implements Onclicklistener {
Global bad environment variables within a member
Private EditText et_main_msg;
Private Button Btn_main_start1;
Private Button Btn_main_start2;
Initializing the View object
Et_main_msg= (EditText) Findviewbyid (r.id.et_main_msg);
btn_main_start1= (Button) Findviewbyid (R.ID.BTN_MAIN_START1);
Btn_main_start2= (Button) Findviewbyid (R.ID.BTN_MAIN_START2);
Set Click to listen
Btn_main_start1.setonclicklistener (this);
Btn_main_start2.setonclicklistener (this);
Callback OnClick method
@Override
public void OnClick (view v) {//v is the (user-manipulated) View object where the event occurred
TODO auto-generated Method Stub
if (V==btn_main_start1) {
Toast.maketext (This, "General startup", Toast.length_short). Show ();
1. Create a Intent object (display)
Intent Intent = new Intent (this,secedeactivity.class);
2. Carry additional data through intent
String message = Et_main_msg.gettext (). toString (). Trim ();
Intent.putextra ("MSG", message);
3. Activate activity
StartActivity (Intent);
}else if (v==btn_main_start2) {
Toast.maketext (This, "with callback start", Toast.length_short). Show ();
1. Create a Intent object (display)
Intent Intent = new Intent (this,secedeactivity.class);
2. By intent
String message = Et_main_msg.gettext (). toString (). Trim ();
Intent.putextra ("MSG", message);
3. Start activity with callback
int requestcode = 4;
Startactivityforresult (Intent, Requestcode);
}
}
Rewrite Onactivityresult
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
Judge Code
if (requestcode==4 && resultcode==6) {
Data extraction
String result = Data.getstringextra ("result");
Show
Et_main_msg.settext (result);
4. Get Intent Object
Intent intent=getintent ();
5. Read additional data
String message = Intent.getstringextra ("MSG");
6. Display in EditText
Et_secede_msg.settext (message);
}
Callback Back1 method passing in a view value
public void Back1 (View v) {
Close the current interface
Finish ();
}
public void Back2 (View v) {
Save a result
int resultcode = 6;
Prepare a intent with additional data
Intent data = new Intent ();
String result = Et_secede_msg.gettext (). toString (). Trim ();
Data.putextra ("result", result);
Set results
Setresult (ResultCode, data);
Close the current interface
Finish ();
Android Getting Started notes