简体   繁体   English

为什么某些android事件停止传播,而其他则不需要?

[英]why do some android events stop propagation and other don't need to?

why do some event listeners return boolean and some don't? 为什么有些事件侦听器返回布尔值,而有些却没有呢?

what is the meaning of stopping propagation ? stopping propagation是什么意思?

for instance: 例如:

public void onClick(View v)

public boolean onTouch(View v, MotionEvent event)

Have a look here: http://developer.android.com/guide/topics/ui/ui-events.html 在这里看看: http : //developer.android.com/guide/topics/ui/ui-events.html
They have a note which says: 他们有一条纸条,上面写着:

Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. 注意:Android将首先调用事件处理程序,然后再从类定义中调用适当的默认处理程序。 As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. 这样,从这些事件侦听器返回true将停止将事件传播到其他事件侦听器,并且还将阻止对View中默认事件处理程序的回调。 So be certain that you want to terminate the event when you return true. 因此,请确保您要在返回true时终止事件。

What I understand about 'stopping propagation' is this: say you have a TextView nested deep in other ViewGroup like RelativeLayout and LinearLayout , a click on the text view is automatically a click on the ViewGroup . 我对“停止传播”的理解是:假设您有一个嵌套在其他ViewGroupRelativeLayoutLinearLayout深处的TextView ,单击文本视图即自动单击ViewGroup This click is then 'propagated' down till it reaches the target text view. 然后,将该点击“传播”​​到目标文本视图。 My understanding is based on this: 我的理解基于此:

Remember that hardware key events are always delivered to the View currently in focus. 请记住,硬件按键事件总是传递给当前关注的View。 They are dispatched starting from the top of the View hierarchy, and then down, until they reach the appropriate destination. 它们从View层次结构的顶部开始分配,然后向下分配,直到到达适当的目的地。 If your View (or a child of your View) currently has focus, then you can see the event travel through the dispatchKeyEvent() method. 如果您的View(或View的子级)当前具有焦点,则可以通过dispatchKeyEvent()方法查看事件传播。 As an alternative to capturing key events through your View, you can also receive all of the events inside your Activity with onKeyDown() and onKeyUp(). 作为通过视图捕获关键事件的替代方法,您还可以使用onKeyDown()和onKeyUp()来接收Activity内部的所有事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有些类需要初始化,有些则不需要 - Some classes need initialization and some don't 为什么有些命令需要在Unix上锁定而其他命令不需要? - Why do some commands require locks on Unix and others don't? 为什么匿名类即使不需要也会捕获“this”? - Why do anonymous classes capture “this” even if they don't need to? 为什么有些类在创建实例时不需要“New”这个词? - Why some classes don't need the word “New” when creating its instance? 为什么 Spring Boot 项目中的某些依赖项不需要指定版本? - Why you don't need to specify the version for some dependencies in Spring Boot project? 放大身份验证事件在 android 中不起作用 - Amplify Auth events don't work in android 为什么 JDK 中的一些可比较类将比较函数限制为 {−1, 0, 1} 而有些则没有? - Why do some comparable classes in the JDK limit the comparison function to {−1, 0, 1} and some don't? 需要 BLUETOOTH 权限:我在某些意图 Android Studio 中没有权限 - Need BLUETOOTH permission : i don't have permission in some intent Android Studio 泛型 - 为什么某些版本不起作用? - Generics - Why don't some versions work? 为什么我不需要创建一个对象? - Why don't I need to create an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM