Android Self-learning process-callback function meaning (source + example)

Source: Internet
Author: User

Why learn the callback function, and then start with the thread. Although previously wrote an article called "Android Self-learning process-multi-threading", now look still too young.

In the learning thread, I tried to look at the source of handler, so the callback interface of the interest. So fill their own lack, take to learn. Stop the nonsense, organize your thoughts, and learn together.

below this is a section of handler source code
    /** * CALLBACK (Recycle) interface (interface) you can use when instantiating (example) a Handler to avoid * have to implement (implementation)     Your own subclass (Asia outline) of Handler. * * @param msg A {@link android.os.Message Message} object * @return True If no further handling is desired 
    */     Public Interface Callback { Publicboolean handlemessage(Message msg); }        /** * Subclasses (Asia outline) must implement (implementation) this to receive messages. */     Public void handlemessage(Message msg) {}

The above Chinese translation is not my translation, translation software do good, not a right ...

Handler believe that everyone has used, when we instantiate a handler, we all say that we want to implement the method, in fact, is to achieve Handlemessage method.

Let's take a look at the source code, the way it is implemented. The first is the interface of the callback method, and since it is an interface, then we must implement all of these methods, both "Handlemessage" methods.

Look at the above I marked in red in English, is not a feeling, don't worry. Let's find an example to look at.

below is a section of Onclicklistener source code
    /**      Interface definition for a callback to being invoked when a view is clicked.       */     Public Interface Onclicklistener {        /**         * Called when a view has been clicked.         *         @param  v The view that is clicked.          */        void OnClick (View v);    }

Let's take a look at the words in red: When a view is clicked, the callback function defined by this interface will be triggered. We do not show the call to the OnClick method, the user triggers the Click event, the system will call this onclick method.

I was on the way to find the source of Setonclicklistener, may be helpful to understand the latter
    /**      * Register A callback to being invoked when the this view is clicked. If This view was not     * clickable, it becomes clickable.     *     @param  L The callback that'll run     *     @see  #setClickable ( Boolean)     *    /publicvoid  Setonclicklistener (onclicklistener L) {        if (!  Isclickable ()) {            setclickable (true);        }         = l;    }

Here, I found a good tutorial that will give us a better understanding of: Using code to simulate registering event listeners

Write a listener interface first

 Package Com.listener;   /**   */   Public Interface Myonclicklistener {      publicvoid  OnClick ();  

Write a button class of our own to achieve the click Effect.

 PackageCom.listener; ' Public classMyButton {PrivateMyonclicklistener Listener; /*** Set Specific click Listener *@paramListener Tap Listener implementation class*/       Public voidSetonclicklistener (Myonclicklistener listener) { This. Listener =Listener; }            /*** button is clicked*/       Public voidDoClick () {Listener.onclick (); }  } 

Finally, the client's registration listener and trigger Click action are simulated.

 PackageCom.listener; Publicclass Client { Public Static voidMain (string[] args) {MyButton button=NewMyButton (); //Register ListenerButton.setonclicklistener (NewMyonclicklistener () {@Override Public voidOnClick () {System.out.println ("button is clicked.");          }                        }); //Simulate user clicksButton.doclick (); }  } 

I have seen this for a while, feel or feel. http://blog.csdn.net/jason0539/article/details/10168899

Its comments I cut a few pictures, still have a good inspiration.

Of course I found the answer to God's level and shared it.

Well, after about the understanding, we can continue to follow up the study.

Android Self-learning process-callback function meaning (source + example)

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.