Android OnClick button Click event Four common wording

Source: Internet
Author: User

Here we use four onclick click events to implement the demo of the phone number.


XML file

<edittext        android: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 "/>    <button        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"/>

The first, anonymous inner class:

protected void OnCreate (Bundle savedinstancestate) {<span style= "White-space:pre" ></span>super.oncreate        (savedinstancestate); Setcontentview (R.layout.activity_main); <span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>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 public        void OnClick (view view) {            Dialphone ();        }    }

The third kind, activity inherits View.onclicklistener, implements the OnClick (view view) method by the activity, in the onclick (view view) The Switch-case method is used to handle the button corresponding to different IDs:

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);    }    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:

<button        android: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);    }

Summarize:

The fourth way to do this is to implement the Click event of a button without declaring the button in the entire code. However, this is not usually recommended, and the third method is the best way to use the onclick. When the button is low, anonymous internal classes will be faster, such as when writing a demo test.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.