How the Android button listens

Source: Internet
Author: User

Several ways to listen for Android button

1, a button corresponding to a monitoring

1) Binding Listener in XML file

< Button         Android:id = "@+id/btn_test"         android:layout_width= "Match_parent"        android:layout_height= "Wrap_ Content "        android:text=" Test Listener "        android:onclick=" Test "        />

Implement this method in the corresponding file

 Public void Test (View v) {        // to does something        toast.maketext (This, "XML Binding Listener Response", Toast.length_short). Show ();}

2)

Button btntest = (Button) Findviewbyid (r.id.btn_test); Btntest.setonclicklistener (new  Onclicklistener () {        @Override    publicvoid  OnClick (View v) {         // TODO auto-generated Method Stub                     }});

2. Multiple buttons correspond to a listener

1)

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (R.layout.activity_main); Button btntest=(Button) Findviewbyid (r.id.btn_test); Button Btndeploy=(Button) Findviewbyid (R.id.btn_deploy);        Btntest.setonclicklistener (listener);  Btndeploy.setonclicklistener (listener); } Onclicklistener Listener=NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub            Switch(V.getid ()) { Caser.id.btn_test: Break;  CaseR.id.btn_deploy: Break; default:                 Break; }                    }    };}

2)

 Public classMainactivityextendsActivityImplementsonclicklistener{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (R.layout.activity_main); Button btntest=(Button) Findviewbyid (r.id.btn_test); Button Btndeploy=(Button) Findviewbyid (R.id.btn_deploy); Btntest.setonclicklistener ( This); Btndeploy.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub        Switch(V.getid ()) { Caser.id.btn_test: Break;  CaseR.id.btn_deploy: Break; default:             Break; }    }}

How the Android button listens

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.