android touch机制,触摸屏虚拟键盘 Android Touch事件传递机制全面解析(从WMS到View树)(5)...

dispatchTouchEvent方法。终于也是会调用到super.dispatchTouchEvent。我们能够继续往下看Activity的dispatchTouchEvent方法:

public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {

onUserInteraction();

}

if (getWindow().superDispatchTouchEvent(ev)) {

return true;

}

return onTouchEvent(ev);

}

前面if分支不用管,这里会调用PhoneWindow的superDispatchTouchEvent方法。进去看看:

@Override

public boolean superDispatchTouchEvent(MotionEvent event) {

return mDecor.superDispatchTouchEvent(event);

}

调用了DecorView的superDispatchTouchEvent方法,再进去看看:

public boolean superDispatchTouchEvent(MotionEvent event) {

return super.dispatchTouchEvent(event);

}

终于还是调用来DecorView的super.dispatchTouchEvent。也就是说。不管如何。DecorView的dispatchTouchEvent终于都会调用到自己父亲FrameLayout的dispatchTouchEvent方法。而我们在FrameLayout中找不到dispatchTouchEvent方法。所以,会去运行ViewGroup的

8f4475068b3894abaa214a5161aa077f.png

dispatchTouchEvent方法。

假设该dispatchTouchEvent返回true,说明后面有view消费掉了该事件,那就返回true,不会再去运行自身的onTouchEvent方法,否则,说明没有view消费掉该事件,会一路回传到Activity中,然后调用自己的onTouchEvent方法。该方法的实现比较简单。例如以下:

public boolean onTouchEvent(MotionEvent event) {

if (mWindow.shouldCloseOnTouch(this, event)) {

finish();

return true;

}

return false;

}

public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {

onUserInteraction();

}

if (getWindow().superDispatchTouchEvent(ev)) {

return true;

}

return onTouchEvent(ev);

}

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/shouji/article-84987-5.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值