When we first approached, I felt that the callback was a difficult concept to understand, and then we kept on surfing the internet to find the definition of the callback: (When you want to put the specific action, let the specific person to complete, you define an interface is good.) When using the interface, the specific person to implement your interface, that is, the implementation of the callback. )
In fact, a careful thought, the OnClick (view view) is a kind of callback, only understand the essence of the principle of things, can be used flexibly.
An example of a custom interface in dialog is given below:
1, the new Xdialog inherit dialog,
2. Define the interface
Public Interface callbacklistener{ void testcall (); void Testcall (Object o); }
3. Defining variables
Private Callbacklistener Callbacklistener; Public void Setcallbacklistener (Callbacklistener callbacklistener) { this. Callbacklistener= Callbacklistener; }
4, in the construction method:
Private intCount = 0; Private voidInitview (Context context) {button Button=(Button) Findviewbyid (R.ID.TEST_BTN); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(NULL!=Callbacklistener) { ++count; Callbacklistener.testcall (count); } } }); }
5. Implement callbacks in Mainactivity:
Callbackdialog Mcallback =NewCallbackdialog (mainactivity. This);//Implementing CallbacksMcallback.setcallbacklistener (NewCallbackdialog.callbacklistener () {@Override Public voidTestcall () {} @Override Public voidTestcall (Object o) {//mtextview.settext (o.tostring ());
DoSomething ();
} });
Android Custom Callbacks