android 分发事件,android事件分发(一)

对于android事件分发是android中的一个难点,很多人对它都一知半解,原因呢只是他们并没有亲自做一个demo,然后认真的去分析一下,只是去网上找一些博客,但博客上对android事件分发的介绍大都篇幅过长,说法也略有差异,所以造成android事件分发成为一个难点。下面,我用一个简单的demo来分析一下android事件分发:

首先自定义了两个类,一个LinearLayout,一个Button:

MyLinearLayout如下:

public class MyLinearLayout extends LinearLayout{

public MyLinearLayout(Context context) {

this(context,null);

}

public MyLinearLayout(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

Log.e("touch","MyLinearLayout-----dispatchTouchEvent---begin");

boolean b = super.dispatchTouchEvent(ev);

Log.e("touch","MyLinearLayout-----dispatchTouchEvent---" + b );

return b;

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

Log.e("touch","MyLinearLayout-----onInterceptTouchEvent---begin");

boolean b = super.onInterceptTouchEvent(ev);

Log.e("touch","MyLinearLayout-----onInterceptTouchEvent---" + b );

return b;

}

}

MyButton如下:

public class MyButton extends Button {

public MyButton(Context context) {

this(context,null);

}

public MyButton(Context context, AttributeSet attrs) {

this(context,attrs,0);

}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

Log.e("touch", "MyButton-----dispatchTouchEvent---begin");

boolean b = super.dispatchTouchEvent(event);

Log.e("touch", "MyButton-----dispatchTouchEvent---" + b);

return b;

}

}

简单说一下:在MyLinearLayout中重写了dispatchTouchEvent和onInterceptTouchEvent,在MyButton中重写了 dispatchTouchEvent,那在MyButton中为什么没有onInterceptTouchEvent呢,因为MyButton继承的是View,View是没有办法事件拦截的,记住:只有ViewGroup才有事件拦截方法。

然后在Activity对应的layout中:

android:id="@+id/layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/button"

android:text="点击"

/>

运行这个demo,然后点击MyButton,在控制台有一下的输出:

03-15 10:19:40.820 3747-3747/com.sumu.gefdemo E/touch: MyLinearLayout-----dispatchTouchEvent---begin

03-15 10:19:40.820 3747-3747/com.sumu.gefdemo E/touch: MyLinearLayout-----onInterceptTouchEvent---begin

03-15 10:19:40.820 3747-3747/com.sumu.gefdemo E/touch: MyLinearLayout-----onInterceptTouchEvent---false

03-15 10:19:40.820 3747-3747/com.sumu.gefdemo E/touch: MyButton-----dispatchTouchEvent---begin

03-15 10:19:40.820 3747-3747/com.sumu.gefdemo E/touch: MyButton-----dispatchTouchEvent---false

03-15 10:19:40.820 3747-3747/com.sumu.gefdemo E/touch: MyLinearLayout-----dispatchTouchEvent---false

解释一下:

1.先是MyLinearLayout中开始执行dispatchTouchEvent方法

2.然后进入MyLinearLayout的onInterceptTouchEvent方法

3.onInterceptTouchEvent返回值为false,然后又进入了MyButton的dispatchTouchEvent

4.MyButton的dispatchTouchEvent返回值是false

5.MyLinearLayout的dispatchTouchEvent执行完毕,返回值为false。

大家先不要管这几个方法的返回值,我们先关注一下执行的流程:如果你点击了一个控件,如果这个控件有父控件,先进入父控件的dispatchTouchEvent,然后再进入父控件的onInterceptTouchEvent方法,当onInterceptTouchEvent返回值为false时,表示事件不拦截,然后又进入到了子控件的dispatchTouchEvent方法进行事件分发,OK,分析完毕。

当我们把MyLinerLayout的onInterceptTouchEvent返回值直接return true:

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

Log.e("touch","MyLinearLayout-----onInterceptTouchEvent---begin");

boolean b = super.onInterceptTouchEvent(ev);

//        Log.e("touch","MyLinearLayout-----onInterceptTouchEvent---" + b );

return true;

}

再看控制台:

03-15 11:00:13.530 21053-21053/com.sumu.gefdemo E/touch: MyLinearLayout-----dispatchTouchEvent---begin

03-15 11:00:13.530 21053-21053/com.sumu.gefdemo E/touch: MyLinearLayout-----onInterceptTouchEvent---begin

03-15 11:00:13.530 21053-21053/com.sumu.gefdemo E/touch: MyLinearLayout-----dispatchTouchEvent---false

OK,MyButton的 dispatchTouchEvent没有执行,表示事件拦截了。

好了,这篇博客的重点就是知道事件分发的流程,下一篇博客再把onTouchEvent加进来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值