Android Getting Started notes

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.