Android 中的接口回调

http://blog.csdn.net/wangjinyu501/article/details/22052187

  在Android中到处可见接口回调机制,尤其是UI事件处理方面。举一个最常见的例子button点击事件,button有一个点击方法onClick(),我们知道onclick()是一个回调方法,当用户点击button就执行这个方法。在源码中是这样定义的:
    //这个是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);  
    }  

 下面看一个简单的例子:

 

    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();  
         }  
      
    }  
        这就是一个很典型的例子,当然也可以这样写:  
    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);    
         }  
      
    }  

 

 下面是View类的setOnClickListener方法,把和回调相关代码贴出来。什么贴它呢,因为Button继承于TextView,而TextView继承于View,在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;  
        }  
    }  

 

那现在一起来总结一下基本的回调是如何实现的,首先创建一个接口,这个接口用于你在某 个情景下执行相应的操作。接着创建一个功能类,比如这个类可以显示一个对话框、可以滑动菜单、可以下载数据等等。然后,在这个类里面声明回调接口的对象, 之后在这个类里面创建在某个情景下需要执行的方法,而且在这个方法里面为声明的接口对象赋值。最后在其他的类中使用这个功能类就可以了。所以说,最少也是 需要三个类共同来完成这个回调机制。
    这下大家应该就比较明白了,那我们就自己按照这个方式和流程完成一个这样的例子。以Dialog为例,一般我们在开发时候,经常会用到Dialog。比如一个弹出框,里面有确认和取消。通常情况下,我们可能会这样写:
    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();  
                  }  
             });  
也就是得到点击对象之后再去调用onClick(),这样有一个缺点就是你每次都要写,不利于重复使用。那我们就可以对此进行一个封装,看代码:
    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 {  
        //创建接口  
        public static interface OnOKClickListener {  
            public void onOKClick();  
        }  
      
        private final Context mContext;  
        private TextView mTitleView;  
        private TextView mTextView;  
      
        private Button mButtonOk;  
        private CharSequence mTitle;  
      
        private CharSequence mText;  
        //生命接口对象  
        private OnOKClickListener mClickListener;  
      
        public AlertInfoDialog(final Context context, final String title,  
                final String text) {  
            super(context, R.style.Dialog);  
            this.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();//调用  
                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 = 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);  
        }  
        //设置监听器 也就是实例化接口  
        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);  
        }  
      
    }  

    方式和上面介绍的一样,感兴趣的朋友可以自己去实现其他效果的。

 

 

posted @ 2015-07-14 08:54  星辰之力  阅读(325)  评论(0编辑  收藏  举报