The callback function of Android development

Source: Internet
Author: User

Written in front, recently a more profound understanding of the return function, now recorded as follows. Because the home can not see the underlying code, in the company cannot access the network, now can only resorting memory to write this blog post, the wrong place also hope that the gods to point out

In fact, to set the component listener is the most typical example of the return function. This specifically looks at the bottom level of the control view control.

    1. In View.java, various interfaces are defined, such as Onclicklistener, Ontouchlistener, Onlongclicklistener, Onscrolllistener, Ondraglistener and many other interfaces
    2. The corresponding set method and global variable Monclicklistener are then set in View.java and assigned to Monclicklistener in the set method. For example: Setonclicklistener (Onclicklistener listener).
    3. Finally, in View.java to have (theoretically) call the method in this interface, the attention to determine whether the global variable monclicklistener is empty. -for example, to call in Ontouchevent

This completes the definition of the interface (back-off). It can also be called a functional (method) theoretical process. Because after all, it just defines the interface. The actual process of the method, that is, the actual operation is in another class, for example, we usually to a button to Setonclicklistener (Onclicklistener listener), is the implementation of the interface, also known as the return of the interface.

To return the interface, do the following in another class:

    1. Guide Package Eg:import Android.view.View.OnClickListener;
    2. The interface must be set when the control is initialized eg: Btnbutton.setonclicklistener (new Onclicklistener ()) When the button is initialized in OnCreate;--for example, in OnCreate
    3. To implement the methods in the interface.

Previously summed up the four methods of event monitoring, due to event monitoring and return is a principle, so set back the function also has four ways, see

Overview of four event monitoring methods for Android Click button

The callback function of Android development

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.