android事件分析(二)——MOTIONEVENT事件在ONINTERCEPTTOUCHEVENT()、ONTOUCHEVENT()中的传递顺序

关于这个返回值的问题,上一篇文章中那位大牛有很详细的解释。原文链接:http://www.cnblogs.com/kingcent/archive/2011/03/08/1977064.html

我读了之后,结合调试程序,是很好的理解了,所以就不再狗尾续貂了。呵呵

nInterceptTouchEvent()用于处理事件并改变事件的传递方向。处理事件这个不用说了,你在函数内部编写代码处理就可以了。而决定传递方向的是返回值,返回为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)。

onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。

01 <?xml version="1.0" encoding="utf-8"?>
02 <com.touchstudy.LayoutView1xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent" >
06 <com.touchstudy.LayoutView2
07 android:orientation="vertical"
08         android:layout_width="fill_parent"
09         android:layout_height="fill_parent"
10         android:gravity="center">
11 <com.touchstudy.MyTextView
12 android:layout_width="wrap_content"
13             android:layout_height="wrap_content"
14             android:id="@+id/tv"
15             android:text="AB"
16             android:textSize="40sp"
17             android:textStyle="bold"
18             android:background="#FFFFFF"
19             android:textColor="#0000FF"/>
20 </com.touchstudy.LayoutView2>
21 </com.touchstudy.LayoutView1>

在没有重写onInterceptTouchEvent()和onTouchEvent()的情况下(他们的返回值都是false), 对上面这个布局,MotionEvent事件的传递顺序如下:


当某个控件的onInterceptTouchEvent()返回值为true时,就会发生截断,事件被传到当前控件的onTouchEvent()。如我们将LayoutView2的onInterceptTouchEvent()返回值为true,则传递流程变成:


如果我们同时将LayoutView2的onInterceptTouchEvent()和onTouchEvent()设置成true,那么LayoutView2将消费被传递的事件,同时后续事件(如跟着ACTION_DOWN的ACTION_MOVE或者ACTION_UP)会直接传给LayoutView2的onTouchEvent(),不传给其他任何控件的任何函数。同时传递给子空间一个ACTION_CANCEL事件。传递流程变成(图中没有画出ACTION_CANCEL事件):



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值