dispatchTouchEvent,onUserInteraction,onTouchEvent 调用时序

  • 一、只有一个Activity时:一次拇指点击事件(包括ACTION_DOWN,ACTION_UP),调用时序如下:

  1. dispatchTouchEvent

  2. onUserInteraction

  3. onTouchEvent (ACTION_DOWN)

  4. dispatchTouchEvent 

  5. onTouchEvent (ACTION_UP,不会触发onUserInteraction)

  •  二、只有一个Activity,当键盘键按下时,时序如下

  1. dispatchKeyEvent

  2. onUserInteraction

  3. onKeyDown

  4. dispatchKeyEvent

  5. onUserInteraction

  6. onKeyUp (注意与触摸不同,当松开按键时onUserInteraction也会触发。)

  • 三、Activity里有一个Layout,在Layout里有个按钮。一次拇指触摸点击事件(包括ACTION_DOWN,ACTION_UP),方法逻辑顺序如下:

  1. Activity的dispatchTouchEvent

  2. Activity的onUserInteraction

  3. Layout的dispatchTouchEvent

  4. Layout的onInterceptTouchEvent

  5. Button的onTouchEvent(ACTION_DOWN)

紧跟着是一个ACTION_UP事件

  1. Activity的dispatchTouchEvent(不触发Activity的onUserInteraction,它对ACTION_UP不起作用。)

  2. Layout的dispatchTouchEvent

  3. Layout的onInterceptTouchEvent

  4. Button的onTouchEvent

  5. Button的onClick

如果在onTouchEvent里返回true,消费了该事件,那么onClick将不会被响应。

但是如果不写onClick事件,而onTouchEvent事件返回false,也没有上述时序中的最后一步,其他时序即事件不会再向上传递,估计是onClick有默认响应不处理,而onClick不会有返回值。但是如果是继承了一个View而且又覆写了onTouchEvent,若返回false,那么触控会向上传递。

时序如下

  1. Activity的dispatchTouchEvent

  2. Activity的onUserInteraction

  3. Layout的dispatchTouchEvent

  4. Layout的onInterceptTouchEvent

  5. Button的onTouchEvent (ACTION_DOWN)

  6. Layout的onTouchEvent (ACTION_DOWN)

紧跟着是一个ACTION_UP事件

  1. Activity的dispatchTouchEvent(不触发Activity的onUserInteraction,它对ACTION_UP不起作用。)

  2. Layout的dispatchTouchEvent

  3. Layout的onInterceptTouchEvent

  4. Button的onTouchEvent

onClick事件不再发生。

  •  对于键盘监听

VIEW和Activity本身都有相应键盘事件的ONKEYUP和ONKEYDOWN

对于VIEW,可以setOnKeyListener监听键盘事件,如果既对VIEW写了上述侦听,又覆写了ONKEYUP,DOWN事件,那么首先进入ONKEYDOWN,UP。

REFERENCES:http://lexhsu.lofter.com/post/3f493_1f3348

http://blog.csdn.net/wangtao4226/article/details/6868587

http://blog.csdn.net/ritterliu/article/details/7610486

http://www.cnblogs.com/jqyp/archive/2012/04/25/2469758.html

原文地址:https://www.cnblogs.com/anee/p/2748372.html