Android Listener Events

Source: Internet
Author: User

Three ways to implement wirelessly monitoring events

1. How to implement anonymous inner class

2, the realization of independent class mode

3. Realization of Interface mode

I. Implementation of anonymous internal classes

1. First declare a button

    // Declare a button    Private Button Listener1;

2. Set the Listener for button and implement it by anonymous inner class

  @Override        protected void onCreate (Bundle savedinstancestate) {            super.oncreate (savedinstancestate);            To set the button listener, we can implement the button click Action            listener1= (Button) Findviewbyid (R.ID.BTN1) via the button listener;        /* * Listener events implemented in the first way (anonymous inner Class) * */        Listener1.setonclicklistener (New Onclicklistener () {            @Override Public            void OnClick (View arg0) {                System.out.println ("This is the first way (an implementation of an anonymous inner class) implements a listener event");}        );
}

II. realization of independent class mode

1. First declare a button

// first, declare a button Private Button Listener2;

2. Create an external independent class to listen for events

// create an external stand-alone class to listen for events    Onclicklistener listener=New  Onclicklistener () {        @Override        publicvoid  OnClick (View v) {            System. out. println (" This is the second way (independent class) to implement the listener event ");}    };

3. Implement Monitoring Events

       /*         * Monitoring events by the second way independent class implementation         *        /listener2.setonclicklistener (listener);

III. realization of interface mode

1. Create a button

    // Declare a button    Private Button Listener3;

2. Implement the interface

//Implement Interface Onclicklistener Public classMainactivity extends Actionbaractivity implements Onclicklistener {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Listener3=(Button) Findviewbyid (R.ID.BTN3); /** Listener events implemented in the Third Way (via interface) * 1, first implement implements Onclicklistener interface **/Listener3.setonclicklistener ( This);} //Listener Event Implementation Mode 3: interface mode//implementing the Implements Onclicklistener interface@Override Public voidOnClick (View v) {System. out. println ("This is the Third way (interface mode implementation) to implement the listener event"); }

Android Listener 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.