Android 触摸事件是如何传递的? [英] How are Android touch events delivered?

查看:24
本文介绍了Android 触摸事件是如何传递的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在问如何处理触摸事件,而是在问幕后发生了什么?如果有多个嵌套的小部件,他们看到事件的顺序是什么?开发者有控制权吗?理想情况下,我想要一份关于该主题的文件.

I'm not asking how to handle touch events, but what is going on behind the scenes? If there are several nested widgets, what order do they see the events in? Does the developer have any control over it? Ideally I would like a document on the subject.

推荐答案

从 Activity 的角度:

From Activity viewpoint:

触摸事件首先传递给Activity.dispatchTouchEvent.在这里你可以先抓住它们.

Touch events are delivered first to Activity.dispatchTouchEvent. It's where you may catch them first.

在这里,它们被分派到 Window,在那里它们遍历 View 层次结构,按照最后绘制的 Widget(在其他 Widget 之上)有机会首先处理 View.onTouchEvent 中的触摸.如果某些View在onTouchEvent中返回true,则遍历停止,其他View不会收到触摸事件.

Here they get dispatched to Window, where they traverse View hierarchy, in such order that Widgets that are drawn last (on top of other widgets) have chance to process touch in View.onTouchEvent first. If some View returns true in onTouchEvent, then traversal stops and other Views don't receive touch event.

最后,如果没有 View 消费触摸,则将其传递给 Activity.onTouchEvent.

Finally, if no View consumes touch, it's delivered to Activity.onTouchEvent.

这完全由您控制.合乎逻辑的是,您所看到的绘制在其他东西之上的东西,有机会在绘制在其下方的东西之前处理触摸事件.

That's all your control. And it's logical that what you see drawn on top of something else, has chance to process touch event before something drawn below it.

这篇关于Android 触摸事件是如何传递的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆