[英]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
. 我对“停止传播”的理解是:假设您有一个嵌套在其他
ViewGroup
如RelativeLayout
和LinearLayout
深处的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.