Four common ways to use Android button click events

Source: Internet
Author: User

Many people who study Android programming find that each person has different preferences for how the code is written, and what is more obvious is the difference in how the control responds to events. Therefore this article summarizes these writing, compares each kind of writing the merits and demerits, hoped that everybody flexibly chooses the code way may have the reference value.

The four methods are described as follows:

Anonymous inner class:

 Public classTestbuttonactivityextendsActivity {Button btn1, btn2;   Toast TST; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (R.layout.activity_test_button); BTN1=(Button) Findviewbyid (R.id.button1); BTN2=(Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast TST = Toast.maketext (testbuttonactivity. This, "111111111", Toast.length_short);       Tst.show ();     }    }); Btn2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast TST = Toast.maketext (testbuttonactivity. This, "222222222", Toast.length_short);      Tst.show ();  }    }); }}

Custom Click event Listener class:

 Public classTestbuttonactivityextendsActivity {Button btn1, btn2;   Toast TST; classMyclicklistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub      Switch(V.getid ()) { Caser.id.button1:tst= Toast.maketext (testbuttonactivity. This, "111111111", Toast.length_short);        Tst.show ();  Break;  Caser.id.button2:tst= Toast.maketext (testbuttonactivity. This, "222222222", Toast.length_short);        Tst.show ();  Break; default:         Break; } }} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (R.layout.activity_test_button); BTN1=(Button) Findviewbyid (R.id.button1); BTN2=(Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener (NewMyclicklistener ()); Btn2.setonclicklistener (NewMyclicklistener ()); }}

Activity inherits View.onclicklistener, which is implemented by the activity onclick (view view) method, in the onclick (view view) The method uses Switch-case to handle the button corresponding to different ID.

 Public classTestbuttonactivityextendsActivityImplementsOnclicklistener {Button btn1, btn2;   Toast TST; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (R.layout.activity_test_button); BTN1=(Button) Findviewbyid (R.id.button1); BTN2=(Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener ( This); Btn2.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub    Switch(V.getid ()) { Caser.id.button1:tst= Toast.maketext ( This, "111111111", Toast.length_short);      Tst.show ();  Break;  Caser.id.button2:tst= Toast.maketext ( This, "222222222", Toast.length_short);      Tst.show ();  Break; default:       Break; }  }}

The last one is to "display 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:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "Button1" />   <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "Button2" />

Here in the end of the android: Press alt+/will have the OnClick property prompt, but the input to android:onclick= "place pressed alt+/and did not prompt the OnClick option, let me suddenly feel there seems to be a problem.

 Public classTestbuttonactivityextendsActivity {Button btn1, btn2;   Toast TST; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);  Setcontentview (R.layout.activity_test_button); }   //notice there's no @Override tag here .   Public voidOnClick (View v) {//TODO auto-generated Method Stub    Switch(V.getid ()) { Caser.id.button1:tst= Toast.maketext ( This, "111111111", Toast.length_short);      Tst.show ();  Break;  Caser.id.button2:tst= Toast.maketext ( This, "222222222", Toast.length_short);      Tst.show ();  Break; default:       Break; }  }}

In this case, the button's Click event can be implemented without declaring the button in the entire code.

These are the four ways to implement a button click event.

A rough summary, that is, when the button is less time with anonymous internal classes will be faster, such as writing a demo test or landing interface and so on.

Button A lot of cases I still choose a third method, convenient.

About the fourth method, I feel the most convenient, but read a lot of code or feel that the wording is not popular, interested friends can study this. I believe there will be a lot of gains.

Four common ways to use Android button click events

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.