Android self-learning course-callback function meaning (source code + example), android callback function

Source: Internet
Author: User

Android self-learning course-callback function meaning (source code + example), android callback function

Why do we learn the callback function? Let's start with the thread. Although I have written an article called "Android self-learning process-multithreading" before, I am still too young.

When I was learning the thread, I tried to look at the Handler source code, so I became interested in the Callback interface. So I made up my own shortcomings and used them for learning. So far, let's sort out ideas and learn together.

 

The following is the source code of Handler.
/*** Callback interface (interface) you can use when instantiating (example) a Handler to avoid * having to implement (Implementation) your own subclass (sub-class) of Handler. ** @ param msg A {@ link android. OS. message} object * @ return True if no further handling is desired */public interfaceCallback{Public boolean handleMessage (Message msg);}/*** Subclasses (sub-class) must implement (Implementation) this to receive messages. */public voidHandleMessage(Message msg ){}

The Chinese translation above is not what I translated. The translation software did a good job, and there was no such thing as correct...

 

Handler is believed to have been used. When we instantiate a Handler, we all say that we want to implement the HandleMessage method.

Let's take a look at the source code. The first is the interface of the Callback method. Since it is an interface, we must implement all the methods in it, both the "handleMessage" method.

 

Let's take a look at the English text I marked in red. Do you feel it? Don't worry. Let's look at an example.

The following is the source code of OnClickListener.
    /**     * Interface definition for a callback to be invoked when a view is clicked.     */    public interface OnClickListener {        /**         * Called when a view has been clicked.         *         * @param v The view that was clicked.         */        void onClick(View v);    }

 

Let's take a look at the Red Section: When a View is clicked, the callback function defined by this interface will be triggered. The OnClick method is not displayed. The user triggers this click event,The system will callThis OnClick method.

I have found the source code of setOnClickListener, which may be helpful for later understanding.
    /**     * Register a callback to be invoked when this view is clicked. If this view is not     * clickable, it becomes clickable.     *     * @param l The callback that will run     *     * @see #setClickable(boolean)     */    public void setOnClickListener(OnClickListener l) {        if (!isClickable()) {            setClickable(true);        }        getListenerInfo().mOnClickListener = l;    }

 

Here, I found a good tutorial to give us a better understanding: using code to simulate event listener Registration

First write a listener Interface

Package com. listener;/*** click listener interface */public interface MyOnClickListener {public void onClick ();}

 

 

Write a Button class to achieve the click effect.

Package com. listener; listener public class MyButton {private MyOnClickListener listener;/*** set click listener * @ param listener click listener implementation class */public void setOnClickListener (MyOnClickListener listener) {this. listener = listener;}/*** button is clicked */public void doClick () {listener. onClick ();}}

 

 

Finally, register the listener on the Client side and trigger the click operation.

Package com. listener; publicclass Client {public static void main (String [] args) {MyButton button = new MyButton (); // register the listener button. setOnClickListener (new MyOnClickListener () {@ Override public void onClick () {System. out. println ("the button has been clicked") ;}}); // simulate the user clicking the button. doClick ();}}

 

After reading this for a while, I still feel it. Http://blog.csdn.net/jason0539/article/details/10168899

I have taken several pictures of its comments, which is a good inspiration.

 

 

Of course, I also found a God-class answer in zhihu, sharing it.

 

Okay. After a rough understanding, we can continue our study.

 

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.