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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3756383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:40:17  来源:igfitidea点击:

What is meaning of boolean value returned from an event-handling method in Android

androideventstouchreturnlistener

提问by John Wang

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 ?

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

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

Regarding to the above example, if return true in onTouchmethod,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 ?

关于上面的例子,如果在onTouch方法中返回 true ,我发现每个触摸事件(DOWN、UP、MOVE 等)都已根据我的logView被捕获。相反,如果返回false,则只有DOWN事件被捕获。所以似乎 return false 会阻止事件传播。我对么 ?

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

此外,在OnGestureListener 中,许多方法也必须返回一个布尔值。它们的含义相同吗?

回答by adamp

If you return truefrom an ACTION_DOWNevent you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UPor ACTION_CANCEL. Returning falsefrom an ACTION_DOWNmeans 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.

如果您true从一个ACTION_DOWN事件返回,您对该手势中的其余事件感兴趣。在这种情况下,“手势”是指直到最终ACTION_UP或 的所有事件ACTION_CANCEL。返回falseACTION_DOWN意味着你不希望事件和其他视图将有机会来处理它。如果您有重叠视图,这可以是同级视图。如果不是,它将冒泡到父级。

回答by Matthieu

From the documentation : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)

从文档:http: //developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)

"True if the listener has consumed the event, false otherwise."

“如果侦听器已经消费了事件,则为真,否则为假。”

If you return true, the event is processed. If false, it will go to the next layer down.

如果返回 true,则处理该事件。如果为假,它将向下一层。

回答by Falmarri

The boolean value determines whether the event is consumed or not.

布尔值决定事件是否被消费。

Yes you're correct. If you return false, the next listener handles the event. If it returns true, the event is consumed by your listener and not sent to the next method.

是的,你是对的。如果返回 false,下一个侦听器将处理该事件。如果它返回 true,则该事件由您的侦听器使用,而不是发送到下一个方法。

回答by Phan Van Linh

All above answer is correct but the result is different is if the View is clickableor not clickable

以上所有的答案是正确的,但结果不同的是,如果该视图clickable或不clickable

Example, I have a LinearLayoutcontains 1 Buttonand 1 TextViewlike this

例如,我有一个LinearLayout包含 1Button和 1TextView这样的

<LinearLayout
    android:id="@+id/linearlayout_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0aa"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:text="Button Click"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textview_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:text="TextView Click"
        android:textSize="20sp"
        android:background="#e4e4e4"
        />

</LinearLayout>

In Activity, I have code like

在活动中,我有这样的代码

class MainActivity : AppCompatActivity() {
    val TAG = "TAG"

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<LinearLayout>(R.id.linearlayout_root).setOnTouchListener { v, event ->
            Log.i(TAG, "LinearLayout onTouch event " + getDisplayAction(event.action))
            false
        }

        findViewById<Button>(R.id.button_click).setOnTouchListener { v, event ->
            Log.i(TAG, "Button onTouch event " + getDisplayAction(event.action))
            false
        }

        findViewById<TextView>(R.id.textview_click).setOnTouchListener { v, event ->
            Log.i(TAG, "TextView onTouch event " + getDisplayAction(event.action))
            false
        }
    }

    private fun getDisplayAction(action: Int): String {
        return when (action) {
            MotionEvent.ACTION_DOWN -> "DOWN"
            MotionEvent.ACTION_MOVE -> "MOVE"
            MotionEvent.ACTION_UP -> "UP"
            MotionEvent.ACTION_CANCEL -> "CANCEL"
            MotionEvent.ACTION_OUTSIDE -> "OUTSIDE"
            else -> "UNKNOWN"
        }
    }
}

Case 1 Linear onTouch return **FALSE**, Button onTouch return **FALSE**, TextView onTouch return **FALSE**

情况 1 Linear onTouch return **FALSE**, Button onTouch return **FALSE**,TextView onTouch return **FALSE**

Click on Button

点击按钮

I/TAG: Button onTouch eventDOWN
I/TAG: Button onTouch eventMOVE
I/TAG: Button onTouch eventUP

Click on TextView

单击文本视图

TAG: TextView onTouch eventDOWN
TAG: LinearLayout onTouch eventDOWN

Click on LinearLayout

单击线性布局

TAG: LinearLayout onTouch eventDOWN

Case 2 Linear onTouch return **FALSE**, Button onTouch return **TRUE**, TextView onTouch return **TRUE**

情况 2 Linear onTouch return **FALSE**, Button onTouch return **TRUE**,TextView onTouch return **TRUE**

Click on Button

点击按钮

Similar to case 1

Click on TextView

单击文本视图

TAG: TextView onTouch event DOWN
TAG: TextView onTouch event MOVE
TAG: TextView onTouch event UP

Click on LinearLayout

单击线性布局

Similar to case 1

Case 3 Linear onTouch return **TRUE**, Button onTouch return **FALSE**, TextView onTouch return **FALSE**

情况 3 Linear onTouch return **TRUE**, Button onTouch return **FALSE**,TextView onTouch return **FALSE**

Click on Button

点击按钮

Similar to case 1

Click on TextView

单击文本视图

TAG: TextView onTouch event DOWN
TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP

Click on LinearLayout

单击线性布局

TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP

Note

笔记

  • Default of TextViewis not clickable, it will become clickable if we set android:clickable="true"in xml ORwhen we set textView.setOnClickListener(...)
  • When you debug, event MOVEcan call more than my log (it base on how you tap)
  • 默认为TextViewis not clickable,如果我们android:clickable="true"在 xml 中设置或者当我们设置时它会变得可点击textView.setOnClickListener(...)
  • 调试时,event MOVE可以调用的不仅仅是我的日志(它基于你的点击方式)

Summary

概括

  • onTouchreturn trueor view is clickable, View will receive allonTouchEvent
  • onTouchreturn falseand view is not clickable, view will not receive NEXTonTouchEvent (it's parent may receive it)
  • onTouchreturntrue或 view is clickable, View 将接收所有onTouchEvent
  • onTouchreturnfalse和 view is not clickable,view不会收到 NEXTonTouchEvent (它的父级可能会收到它)

Hope it help
DEMO

希望对DEMO有帮助

回答by kamor

I lost nearly one day in troubleshooting, still i found out, that my onTouch function is called 2 times when using true and 1 times when using false.

我在故障排除中损失了将近一天,但我仍然发现,我的 onTouch 函数在使用 true 时被调用了 2 次,在使用 false 时被调用了 1 次。

回答by huynq0911

From Android-document:

Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. 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. So be certain that you want to terminate the event when you return true.

来自Android 文档

注意:Android 将首先调用事件处理程序,然后是类定义中的适当默认处理程序。因此,从这些事件侦听器返回 true 将停止将事件传播到其他事件侦听器,并且还将阻止回调到视图中的默认事件处理程序。所以一定要在返回 true 时终止事件。