Android Button 的回调机制分析

在我搜索学习回调机制时,找到这篇博客http://blog.csdn.net/xiaanming/article/details/17483273 ,它里面讲到Button是极好的回调机制的例子,于是我进行了深入的了解了一下,Button到底是怎样工作的

Button的一般设置形式:

1     button.setOnClickListener(new View.OnClickListener() {
2             @Override
3             public void onClick(View v) {
4                 
5             }
6         });

打开android.jar,在包android.widget找到类Button.class:

 1 public class Button extends TextView {
 2     public Button(Context context) {
 3         this(context, null);
 4     }
 5 
 6     public Button(Context context, AttributeSet attrs) {
 7         this(context, attrs, com.android.internal.R.attr.buttonStyle);
 8     }
 9 
10     public Button(Context context, AttributeSet attrs, int defStyle) {
11         super(context, attrs, defStyle);
12     }
13 }

Button继承自TextView,继续在包内找类TextView.class:

1 public class TextView extends View implements ViewTreeObserver.OnPreDrawListener 

TextView继承自View,贴出与Button相关的代码:

 1 public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
 2 AccessibilityEventSource {
 3 
 4 /**
 5      * Register a callback to be invoked when this view is clicked. If this view is not
 6      * clickable, it becomes clickable.
 7      *
 8      * @param l The callback that will run
 9      *
10      * @see #setClickable(boolean)
11      */
12     public void setOnClickListener(OnClickListener l) {
13         if (!isClickable()) {
14             setClickable(true);
15         }
16         getListenerInfo().mOnClickListener = l;
17     }
18 
19   /**
20      * Call this view's OnClickListener, if it is defined.  Performs all normal
21      * actions associated with clicking: reporting accessibility event, playing
22      * a sound, etc.
23      *
24      * @return True there was an assigned OnClickListener that was called, false
25      *         otherwise is returned.
26      */
27     public boolean performClick() {
28         sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
29 
30         ListenerInfo li = mListenerInfo;
31         if (li != null && li.mOnClickListener != null) {
32             playSoundEffect(SoundEffectConstants.CLICK);
33             li.mOnClickListener.onClick(this);
34             return true;
35         }
36 
37         return false;
38     }
39 
40   /**
41      * Call this view's OnClickListener, if it is defined.  Performs all normal
42      * actions associated with clicking: reporting accessibility event, playing
43      * a sound, etc.
44      *
45      * @return True there was an assigned OnClickListener that was called, false
46      *         otherwise is returned.
47      */
48     public boolean performClick() {
49         sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
50 
51         ListenerInfo li = mListenerInfo;
52         if (li != null && li.mOnClickListener != null) {
53             playSoundEffect(SoundEffectConstants.CLICK);
54             li.mOnClickListener.onClick(this);
55             return true;
56         }
57 
58         return false;
59     }
60 }

 

posted @ 2015-07-29 15:28  gewasi  阅读(339)  评论(0)    收藏  举报