The best way to thoroughly understand wirelessly callbacks is to write a similar implementation of the callback function in Android.
I wrote a project to implement the Setonclicklistener callback myself:
Specific directory:
Project source specific address: HTTP://PAN.BAIDU.COM/S/1C0HWPKK,
View.java: Simulates the View control in Android,
1 Packagecom.wyl.component;2 3 ImportCom.wyl.IntP.OnClickListener;4 5 Public classView {6 PublicOnclicklistener Listener;7 PublicString type;8 9 /**Ten * Two parameters of the constructor One * @paramListener A * @paramtype - */ - PublicView (Onclicklistener Listener, String type) { the Super(); - //This.listener = listener; - This. Type =type; - } + - PublicView (String type) { + Super(); A This. Type =type; at } - - /** - * A constructor for a parameter - * - * @paramListener in * The incoming object is an interface, - */ to PublicView (Onclicklistener listener) { + Super(); - This. Listener =Listener; the } * $ PublicOnclicklistener Getlistener () {Panax Notoginseng returnListener; - } the /** + * This method is particularly important, A * @paramListener the */ + Public voidSetlistener (Onclicklistener listener) { - This. Listener =Listener; $ } $ - PublicString GetType () { - returntype; the } - Wuyi Public voidsetType (String type) { the This. Type =type; - } Wu - /** About * The Click () method simulates the Click event of a button and the OnClick () method of the interface is triggered by a single click $ */ - Public voidClick () { - if(Listener! =NULL) { - /* A * Only if the member variable listener of the View object (Control object, such as button, etc.) is not empty + * Callback The OnClick method of the Listener interface object, which is exactly the variable of the view type the */ -Listener. Onclick ( This); $}Else { the //if the member variable listener of the View object (Control object, such as button, etc.) is empty, then the capture will cause an error theSystem.out.println ("To register listener first ....")); the return; the } - } in}
Onclicklistener.java, is an interface,
1 PackageCom.wyl.IntP;2 3 ImportCom.wyl.component.View;4 /**5 * Simulates the Onclicklistener interface in Android,6 * There is only one onclick () method in this interface,7 * @authorAdministrator8 *9 */Ten Public InterfaceOnclicklistener { One Public voidOnclick (view view); A}
Test class:
1 Packagecom.wyl.test;2 3 ImportCom.wyl.IntP.OnClickListener;4 ImportCom.wyl.component.View;5 6 Public classTestclick {7 Public Static voidMain (string[] args) {8View V1 =NewView ("ListView");9V1.setlistener (NewOnclicklistener () {Ten One @Override A Public voidOnclick (view view) { - //TODO auto-generated Method Stub -System.out.println (the type of the V1 control is: "+View.gettype ()); the } - }); -V1. Click ();//Click () is equivalent to an event triggered when clicking on a button in Android, simulating a click event - } +}
Operation Result:
The type of V1 control is: ListView
Android Callback Simulation