Android to add listener events to the button

Source: Internet
Author: User

In Android development, if you want to add a listener event to a button, there are three ways to implement it.

1, the way one

 Public classMainactivity extends Actionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //Get button ButtonsButton btncall=(Button) Findviewbyid (R.id.btncall); //to add a listener event for the button buttonsBtncall.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubCall ();                    }        }); }         /** * Method*/     Public voidCall () {System. out. println ("Make a phone call"); }}

2, Mode two

 Public classMainActivity2 extends Actionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Find buttonButton Btncall =(Button) Findviewbyid (R.id.btncall); //Monitor events for button ButtonsBtncall.setonclicklistener (NewMyonclicklisten ()); }    /** * is the class that implements Onclicklistener * * @author Administrator **/    classMyonclicklisten implements Onclicklistener {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubCall (); }    }    /** * Method*/     Public voidCall () {System. out. println ("Make a phone call"); }}

3, Mode three

 Public classMainActivity3 extends Actionbaractivity implements Onclicklistener//To implement Oncicklistener this interface {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //Find the buttonButton btncall=(Button) Findviewbyid (R.id.btncall); //button Set Listener eventBtncall.setonclicklistener ( This); }         //Method     Public voidCall () {System. out. println ("Make a phone call"); }    //implementing methods in the Onclicklistenr interface@Override Public voidOnClick (View v) {//TODO auto-generated Method StubCall (); }}

Android to add listener events to the button

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.