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.
- In View.java, various interfaces are defined, such as Onclicklistener, Ontouchlistener, Onlongclicklistener, Onscrolllistener, Ondraglistener and many other interfaces
- 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).
- 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:
- Guide Package Eg:import Android.view.View.OnClickListener;
- 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
- 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