从Android中的事件处理方法返回的布尔值是什么意思

What is meaning of boolean value returned from an event-handling method in Android(从Android中的事件处理方法返回的布尔值是什么意思)

本文介绍了从Android中的事件处理方法返回的布尔值是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 android 中,大多数事件监听器方法返回一个布尔值.那个真/假值是什么意思?会对后续事件产生什么影响?

In android, most event listener methods return a boolean value. What is that true/false value mean ? what will it result in to the subsequence events ?

class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        logView.showEvent(event);
        return true;
    }
}

对于上面的例子,如果在onTouch方法中返回true,我发现每个触摸事件(DOWN,UP,MOVE等)都已经根据我的logView.相反,如果返回false,则只捕获DOWN事件.因此,似乎 return false 会阻止事件传播.我说的对吗?

Regarding to the above example, if return true in onTouch method,I found every touch event(DOWN,UP,MOVE,etc) has been captured according to my logView. On the contrary,if return false, onely the DOWN event been captured. So it's seemd that return false will prevent the event to propagate. Am I correct ?

此外,在 OnGestureListener 中,许多方法也必须返回一个布尔值.意思一样吗?

Furthermore, in a OnGestureListener, many methods have to return a boolean value too. Do they have the same meaning ?

推荐答案

如果您从 ACTION_DOWN 事件中返回 true,则您对该事件中的其余事件感兴趣手势.在这种情况下,手势"是指直到最后的 ACTION_UPACTION_CANCEL 之前的所有事件.从 ACTION_DOWN 返回 false 意味着您不希望该事件,其他视图将有机会处理它.如果您有重叠的视图,这可以是同级视图.如果不是,它将冒泡到父级.

If you return true from an ACTION_DOWN event you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UP or ACTION_CANCEL. Returning false from an ACTION_DOWN means you do not want the event and other views will have the opportunity to handle it. If you have overlapping views this can be a sibling view. If not it will bubble up to the parent.

这篇关于从Android中的事件处理方法返回的布尔值是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从Android中的事件处理方法返回的布尔值是什么意思

基础教程推荐