Version: 1.0 Date: 2014.3.25 copyright:©2014 kince reprinted to indicate the source
The interface callback mechanism is visible everywhere in Android, especially in UI event processing. The most common example is the button click event. The button has an onClick () method. We know that onclick () is a callback method. When you click a button, this method is executed. The source code is defined as follows:
// This is a callback Interface of View/*** Interface definition for a callback to be invoked when a view is clicked. */public interface OnClickListener {/*** Called when a view has been clicked. ** @ param v The view that was clicked. */void onClick (View v );}The following is 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 class MainActivity extends Activity implements OnClickListener {private Button button; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button1); button. setOnClickListener (this) ;}@ Override public void onClick (View v) {Toast. makeText (getApplication (), "OnClick", Toast. LENGTH_LONG ). show () ;}} is a typical example. 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 class SSSS extends Activity {private Button button; private OnClickListener clickListener = new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub}; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); button = (Button) findViewById (R. id. button1); button. setOnClickListener (clickListener );}}The following is the setOnClickListener method of the View class. The Code related to the callback is pasted. What should I do with it? Because the Button inherits from TextView, while TextView inherits from View, the callback processed in View:
/****/public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource { /** * Listener used to dispatch click events. * This field should be made private, so it is hidden from the SDK. * {@hide} */ protected OnClickListener mOnClickListener; /** * * Register a callback to be invoked when this view is clicked. If this view is not * clickable, it becomes clickable. * * @param l The callback that will run * * @see #setClickable(boolean) */ public void setOnClickListener(OnClickListener l) { if (!isClickable()) { setClickable(true); } mOnClickListener = l; } /** * Call this view's OnClickListener, if it is defined. * * @return True there was an assigned OnClickListener that was called, false * otherwise is returned. */ public boolean performClick() { sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); if (mOnClickListener != null) { playSoundEffect(SoundEffectConstants.CLICK); mOnClickListener.onClick(this); return true; } return false; }}Now let's take a look at how the basic callback is implemented. First, create an interface for you to execute the corresponding operations in a specific scenario. Create a function class. For example, this class can display a dialog box, slide menu, and download data. Then, declare the object of the callback interface in this class, and then create the method to be executed in a certain scenario in this class, and assign values to the declared interface object in this method. Finally, you can use this function class in other classes. Therefore, at least three classes are required to complete the callback mechanism. Now, we should have understood that we should follow this method and process to complete such an example. Taking Dialog as an example, we usually use Dialog during development. For example, in a pop-up box, there is confirmation and cancellation. In general, we may write as follows:
final Dialog dialog = new Dialog(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(new View.OnClickListener() { @Override public void onClick(View v) { saveUserData(); dialog.dismiss(); TestActivity.this.finish(); } }); ib_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } });That is, call onClick () after obtaining the click object. One drawback is that you have to write the object each time, which is not conducive to repeated use. Then we can encapsulate 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 class AlertInfoDialog extends Dialog implements View. onClickListener {// create interface public static I Nterface OnOKClickListener {public void onOKClick ();} private final Context mContext; private TextView mTitleView; private TextView mTextView; private Button mButtonOk; private CharSequence mTitle; private CharSequence mText; // life interface object private OnOKClickListener mClickListener; public AlertInfoDialog (final Context context, final String title, final String text) {super (context, R. style. dialog); th Is. mContext = context; this. mTitle = title; this. mText = text;} private void init () {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 void onClick (final View v) {final int id = v. getId (); switch (id) {case R. id. button_ OK: cancel (); // call if (this. mClickListener! = Null) {this. mClickListener. onOKClick ();} break; default: break; }}@ Override protected void onCreate (final Bundle savedInstanceState) {super. onCreate (savedInstanceState); setBlurEffect (); init ();} protected void setBlurEffect () {final Window 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 void setMessage (final CharSequence message) {this. mText = message; this. mTextView. setText (this. mText);} public void setMessage (final int resId) {this. mText = this. mContext. getResources (). getText (resId); this. mTextView. setText (this. mText);} // set the listener, that is, the instantiation interface public void setOnClickListener (final OnOKClickListener clickListener) {this. mClickListener = clickListener;} @ Override public void setTitle (final CharSequence title) {this. mTitle = title; this. mTitleView. setText (this. mTitle) ;}@ Override public void setTitle (final int resId) {this. mTitle = this. mContext. getResources (). getText (resId); this. mTitleView. setText (this. mTitle );}}Similar to the method described above, interested friends can achieve other effects on their own.