Android Setonclicklistener callback function understanding

Source: Internet
Author: User

1. Simulate the Java view class:


Package com.test;/** * Created by Dandan on 15-2-7. */public class View {    /** set default id**/public    static final int no_id =-1;    /** sets the ID of each control for the quilt class override **/public    int mID = no_id;    /** Save Click Callback function **/public    onclicklistener Monclicklistener;    Public interface Onclicklistener {        void OnClick (view view);    };    void Setonclicklistener (Onclicklistener onclicklistener) {        this.monclicklistener = Onclicklistener;    };    public int getId () {        return mID;    };} <span style= "color: #ff0000;" ></span>


2. Simulate Android control to inherit view


Package com.test;/** * Created by Dandan on 15-2-7. */public class Animationbutton extends  view{    /**     * Button Subclass simulates the way a user clicks     *     * **/public    void Moniclick () {        this.mid =;        Onclicklistener clicklistener = This.monclicklistener;        Clicklistener.onclick (this);}    }




3. Impersonate a client using the Animationbutton control


Package com.test;/** * Created by Dandan on 15-2-7. */public class Mainactivity {public    static void Main (string[] args) {        Animationbutton Animationbutton = new Anima Tionbutton ();        Animationbutton.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (view view) {                System.out.print (View.getid ());            }        );
<span style= "White-space:pre" ></span>//analog User click button        animationbutton.moniclick ();}    }







Android Setonclicklistener callback function understanding

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.