Android事件处理[转发]
但是对于VIEW来说,我们如果不改变DRAW,不需要继承,所以如果想响应事件,则需要?
当一个视图(如一个按钮)被触摸时,该对象上的 onTouchEvent() 方法会被调用。不过,为了侦听这个事件,你必须扩展这个类并重写该方法。很明显,扩展每个你想使用的视图对象(只是处理一个事件)是荒唐的。这就是为什么视图类也包含了一个嵌套接口的集合,这些接口含有实现起来简单得多的回调函数。这些接口叫做事件侦听器 event listeners ,是用来截获用户和你的界面交互动作的“门票”。?
1.只有一个ACTIVITY得情况:?
? ? 当鼠标键按下时(即触摸)?
? ?首先触发dispatchTouchEvent?
? ?然后触发onUserInteraction?
? ?再次onTouchEvent?
? ?如果是点击的话,紧跟着下列事件(点击分俩步,ACTION_DOWN,ACTION_up)?
??触发dispatchTouchEvent?
??再次onTouchEvent?
??当ACTION_up事件时不会触发onUserInteraction(可查看源代码)?
??当键盘按下时?
首先触发dispatchKeyEvent?
然后触发onUserInteraction?
再次onKeyDown?
如果按下紧接着松开,则是俩步?
紧跟着触发dispatchKeyEvent?
然后触发onUserInteraction?
再次onKeyUp?
注意与触摸不同,当松开按键时onUserInteraction也会触发。?
? ?Activity.dispatchTouchEvent(MotionEvent) - 这允许你的活动可以在分发给窗口之前捕获所有的触摸事件。?
(同理 dispatchKeyEvent)?
onUserInteraction :Called whenever a key, touch, or trackball event is dispatched to the?
? ???* activity.?
2.activity里有一个LAYOUT,在布局里有个按钮。?
如果在按钮上触发一个CLICK事件?
首先触发ACTIVITY的dispatchTouchEvent?
然后触发ACTIVITY的onUserInteraction?
然后触发LAYOUT的dispatchTouchEvent?
然后触发LAYOUT的onInterceptTouchEvent?
然后触发BUTTON的onTouch(这是一个ACTION_DOWN事件)?
紧跟着是一个ACTION_UP事件?
触发ACTIVITY的dispatchTouchEvent?
注意不再触发ACTIVITY的onUserInteraction,因为他对ACTION_UP不起作用。?
然后触发LAYOUT的dispatchTouchEvent?
然后触发LAYOUT的onInterceptTouchEvent?
然后触发BUTTON的onTouch?
最后触发BUTTON的onClick.?
如果你在ONTOUCH事件里返回true,消费了此事件,那么ONCLICK将不会被响应?
但是如果你不写ONCLICK事件,而ONTOUCH事件返回FLASE?
那么最终事件序列:?
11-23 17:19:44.313: INFO/activity(803): dispatchTouchEvent 11-23 17:19:44.313: INFO/activity(803): onUserInteraction 11-23 17:19:44.322: INFO/LinearLayout(803): dispatchTouchEvent 11-23 17:19:44.333: INFO/LinearLayout(803): onInterceptTouchEvent 11-23 17:19:44.341: INFO/button(803): onTouch 11-23 17:19:44.441: INFO/activity(803): dispatchTouchEvent 11-23 17:19:44.451: INFO/LinearLayout(803): dispatchTouchEvent 11-23 17:19:44.451: INFO/LinearLayout(803): onInterceptTouchEvent 11-23 17:19:44.461: INFO/button(803): onTouch?
11-23 17:25:59.691: INFO/activity(831): dispatchTouchEvent 11-23 17:25:59.691: INFO/activity(831): onUserInteraction 11-23 17:25:59.701: INFO/LinearLayout(831): dispatchTouchEvent 11-23 17:25:59.701: INFO/LinearLayout(831): onInterceptTouchEvent 11-23 17:25:59.701: INFO/button(831): onTouch 11-23 17:25:59.701: INFO/Button(831): onTouchEvent 11-23 17:25:59.701: INFO/LinearLayout(831): onTouchEvent 11-23 17:25:59.701: INFO/activity(831): onTouchEvent 11-23 17:25:59.822: INFO/activity(831): dispatchTouchEvent 11-23 17:25:59.822: INFO/activity(831): onTouchEvent?
11-23 17:23:14.392: INFO/activity(803): dispatchKeyEvent 11-23 17:23:14.404: INFO/activity(803): onUserInteraction 11-23 17:23:14.412: INFO/LinearLayout(803): dispatchKeyEvent 11-23 17:23:14.412: INFO/button(803): onKey 11-23 17:23:14.422: INFO/activity(803): onKeyUp?
11-23 17:35:55.692: INFO/activity(831): dispatchKeyEvent 11-23 17:35:55.713: INFO/activity(831): onUserInteraction 11-23 17:35:55.722: INFO/LinearLayout(831): dispatchKeyEvent 11-23 17:35:55.732: INFO/button(831): onKey 11-23 17:35:55.813: INFO/activity(831): dispatchKeyEvent 11-23 17:35:55.824: INFO/activity(831): onUserInteraction 11-23 17:35:55.831: INFO/LinearLayout(831): dispatchKeyEvent 11-23 17:35:55.831: INFO/button(831): onKey 11-23 17:35:55.953: INFO/button(831): onClick?
11-24 09:55:18.601: INFO/activity(951): dispatchKeyEvent 11-24 09:55:18.611: INFO/activity(951): onUserInteraction 11-24 09:55:18.621: INFO/LinearLayout(951): dispatchKeyEvent 11-24 09:55:18.641: INFO/button(951): onKey 11-24 09:55:18.711: INFO/activity(951): dispatchKeyEvent 11-24 09:55:18.756: INFO/activity(951): onUserInteraction 11-24 09:55:18.775: INFO/LinearLayout(951): dispatchKeyEvent 11-24 09:55:18.791: INFO/button(951): onKey 11-24 09:55:18.822: INFO/button(951): onClick??