http://blog.csdn.net/wangjinyu501/article/details/22052187
the interface callback mechanism is seen everywhere in Android, especially in UI event handling. For one of the most common examples of button click events, the button has a click Method onclick (), and we know that the onclick () is a callback method that executes this method when the user taps the button. This is defined in the source code:
// This is a callback interface for view /* * * Interface definition for a callback to being invoked when a view is clicked. */ Public Interface Onclicklistener { /* * * called when a view has been clicked. * * @param v The view that is clicked. */ void OnClick (View v); }
Let's look at a simple example:
import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.Toast; Public classMainactivity extends Activity implements onclicklistener{Privatebutton button; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button1); Button.setonclicklistener ( This); } @Override Public voidOnClick (View v) {toast.maketext (Getapplication (),"OnClick", Toast.length_long). Show (); }} This is a very typical example, of course, you can also write: import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Public classSSSS extends Activity {Privatebutton button; PrivateOnclicklistener Clicklistener =NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate); Button=(Button) Findviewbyid (R.id.button1); Button.setonclicklistener (Clicklistener); } }
The following is the Setonclicklistener method of the view class, which is used to paste the callback-related code. What paste it, because the button inherits from the TextView, and TextView inherits from the view, the callback that is processed in the view:
/** * */ Public classView implements Drawable.callback, Keyevent.callback, Accessibilityeventsource {/** * Listener used to dispatch click events. * This field should was made private, so it's hidden from the SDK. * {@hide}*/ protectedOnclicklistener Monclicklistener; /** * Register a callback to being invoked when the this view is clicked. If This view was not * clickable, it becomes clickable. * * @param l The callback that would run * * @see #setClickable (Boolean)*/ Public voidSetonclicklistener (Onclicklistener l) {if(!isclickable ()) {setclickable (true); } Monclicklistener=l; } /** * Call this view ' s Onclicklistener, if it is defined. * * @return True There was a assigned onclicklistener that were called, false * otherwise is R eturned. */ PublicBoolean PerformClick () {sendaccessibilityevent (accessibilityevent.type_view_clicked); if(Monclicklistener! =NULL) {playsoundeffect (Soundeffectconstants.click); Monclicklistener.onclick ( This); return true; } return false; } }
Now let's summarize how the basic callback is implemented, and first create an interface that you can use to perform the appropriate action in a situation. Then create a function class, such as this class can display a dialog box, you can swipe the menu, you can download data and so on. Then, the object that declares the callback interface in this class, then creates a method within that class that needs to be executed under a scenario, and assigns a value to the declared interface object in this method. Finally, it is possible to use this function class in other classes. So, at least three classes are required to accomplish this callback mechanism together. It should be more clear to us that we should do it ourselves in this way and process. Taking dialog as an example, we often use dialog in development time. For example, a popup box with confirmation and cancellation. Typically, we may write:
Final Dialog Dialog =NewDialog ( This, R.style.mydialogstyle); Dialog.setcontentview (R.layout.dialog_exit_train); Dialog.show (); ImageButton ib_affirm=(ImageButton) dialog. Findviewbyid (r.id.dialog_exit_ib_affirm); ImageButton Ib_cancel=(ImageButton) dialog. Findviewbyid (R.id.dialog_exit_ib_cancel); Ib_affirm.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {saveuserdata (); Dialog.dismiss (); Testactivity. This. Finish (); } }); Ib_cancel.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Dialog.dismiss (); } }); That is , to get a click on the object and then call the OnClick (), so there is a drawback is that you write every time, not conducive to re-use. So we can do a package for this, look at the code:
import Android.app.Dialog; Import Android.content.Context; Import Android.os.Bundle; Import Android.text.TextPaint; Import Android.view.View; Import Android.view.Window; Import Android.view.WindowManager; Import Android.widget.Button; Import Android.widget.TextView; Import COM.FANFOU.APP.OPENSOURCE.R; /** * * */ Public classAlertinfodialog extends Dialog implements View.onclicklistener {//Creating Interfaces Public Static InterfaceOnokclicklistener { Public voidOnokclick (); } Privatefinal Context Mcontext; PrivateTextView Mtitleview; PrivateTextView Mtextview; PrivateButton Mbuttonok; Privatecharsequence Mtitle; Privatecharsequence MText; //Life Interface Object PrivateOnokclicklistener Mclicklistener; PublicAlertinfodialog (Final context context, final string title, final string text) {Super (C Ontext, R.style.dialog); This. Mcontext =context; This. Mtitle =title; This. MText =text; } Private voidinit () {Setcontentview (R.layout.dialog_alert); This. Mtitleview =(TextView) Findviewbyid (r.id.title); Final Textpaint TP= This. Mtitleview.getpaint (); Tp.setfakeboldtext (true); This. Mtitleview.settext ( This. Mtitle); This. Mtextview =(TextView) Findviewbyid (R.id.text); This. Mtextview.settext ( This. MText); This. Mbuttonok =(Button) Findviewbyid (R.ID.BUTTON_OK); This. Mbuttonok.setonclicklistener ( This); } @Override Public voidOnClick (Final View v) {finalintID =V.getid (); Switch(ID) { CaseR.id.button_ok:cancel ();//called if( This. mclicklistener! =NULL) { This. Mclicklistener.onokclick (); } Break; default: Break; }} @Overrideprotected voidonCreate (Final Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setblureffect (); Init (); } protected voidSetblureffect () {Final window window=GetWindow (); Final Windowmanager.layoutparams LP=window.getattributes (); //lp.alpha=0.8f; Lp.dimamount =0.6f; Window.setattributes (LP); Window.addflags (WindowManager.LayoutParams.FLAG_DIM_BEHIND); //window.addflags (WindowManager.LayoutParams.FLAG_BLUR_BEHIND); } Public voidsetmessage (final charsequence message) { This. MText =message; This. Mtextview.settext ( This. MText); } Public voidSetmessage (finalintresId) { This. MText = This. Mcontext.getresources (). GetText (ResId); This. Mtextview.settext ( This. MText); } //setting up the listener is the instantiation of the interface Public voidSetonclicklistener (Final Onokclicklistener clicklistener) { This. Mclicklistener =Clicklistener; } @Override Public voidsettitle (final charsequence title) { This. Mtitle =title; This. Mtitleview.settext ( This. Mtitle); } @Override Public voidSettitle (finalintresId) { This. Mtitle = This. Mcontext.getresources (). GetText (ResId); This. Mtitleview.settext ( This. Mtitle); } }
In the same way as described above, interested friends can do their own to achieve other effects.
Interface callbacks in Android