Android OnClick button Click event Four common wording

Source: Internet
Author: User

XML file

<EditTextAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Phone"Android:ems= "Ten"Android:id= "@+id/et_number"Android:layout_alignparenttop= "true"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/dial_number"Android:id= "@+id/bt_dial"Android:layout_below= "@id/et_number"Android:layout_alignright= "@id/et_number"Android:layout_alignend= "@id/et_number" />

The first, anonymous inner class:

protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);      Setcontentview (R.layout.activity_main);      Button bt_dial = (button) Findviewbyid (r.id.bt_dial);              Bt_dial.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {          Dialphone ();  }      }); }

The second, custom click event Listener class:

Protected void oncreate (bundle savedinstancestate)  {       Super.oncreate (savedinstancestate);           setcontentview ( R.layout.activity_main);          et_number =  ( EditText)  mainactivity.this.findviewbyid (r.id.et_number);                                  Button bt_dial =  (Button)  findviewbyid (r.id.bt_dial);           bt_dial.setonclicklistener (New MyListener ());       }        private class  mylistener implements view.onclicklistener {       @Override      &Nbsp; public void onclick (View view)  {           dialphone ();      }  }  

Third, activity inherits View.onclicklistener, which is implemented by the activity onclick (View View) method, the button that is represented by a different ID is handled with switch-case in the onclick (view) method:

public class mainactivity extends activity implements view.onclicklistener  {      edittext et_number;       @Override       protected void oncreate (bundle savedinstancestate)  {           super.oncreate (savedinstancestate);           setcontentview (R.layout.activity_main);           et_number =  (EditText)  mainactivity.this.findviewbyid (R.id.et_number);                 Button bt_dial =  (Button)  findviewbyid (r.id.bt_dial);           bt_dial.setonclicklistener (This);         }    &nBsp;   public void onclick (View view)  {             switch  (View.getid ()) {               case R.id.bt_dial:                   dialphone ();                   break;               default:                   break;           }      }  }  

The fourth type displays the OnClick property of the specified button in the XML file, so that when the button is clicked, the Click () method in the corresponding activity is invoked using reflection:

<ButtonAndroid:onclick= "Dialphonemethod"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/dial_number"Android:id= "@+id/bt_dial"Android:layout_below= "@id/et_number"Android:layout_alignright= "@id/et_number"Android:layout_alignend= "@id/et_number" /> 
public void Dialphonemethod (view view) {Dialphone ();        private void Dialphone () {String number = Et_number.gettext (). toString (). Trim ();          if (textutils.isempty) {Toast.maketext (this, "phone number cannot be empty", Toast.length_long). Show ();      Return      } Intent Intent = new Intent ();      Intent.setaction (Intent.action_call);      Intent.setdata (Uri.parse ("Tel:" + number));  StartActivity (Intent); }

  

Android OnClick button Click event Four common wording

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.