1

我试图围绕Android如何处理触摸事件而陷入困境,对此我感到困惑。据我所知,如果onTouch()视图元素的事件设置为true,则意味着该元素已准备好处理该触摸事件,即它消耗了触摸事件。
如果是这样,在下面的代码片段中,在运行后,锁定,当它返回 true 时,不允许我们更改无线电组的选择
我确定是因为我不太熟悉 android 触摸事件的方式工作。有人可以为我正确总结一下。并解释为什么返回true事件onTouch()会阻止我们选择新项目。

package com.examples.customtouch;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;

/**
 * Created by Dave Smith
 * Double Encore, Inc.
 * Date: 9/25/12
 * TouchListenerActivity
 */

public class TouchListenerActivity extends Activity implements View.OnTouchListener {

    /* Views to display last seen touch event */
    CheckBox mLockBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.touch_listener);

        mLockBox = (CheckBox) findViewById(R.id.checkbox_lock);

        findViewById(R.id.selection_first).setOnTouchListener(this);
        findViewById(R.id.selection_second).setOnTouchListener(this);
        findViewById(R.id.selection_third).setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        /*
         * Consume the events here so the buttons cannot process them
         * if the CheckBox in the UI is checked
         */
        return mLockBox.isChecked();
    }

    private String getNameForEvent(MotionEvent event) {
        String action = "";
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                action = "ACTION_DOWN";
                break;
            case MotionEvent.ACTION_CANCEL:
                action = "ACTION_CANCEL";
                break;
            case MotionEvent.ACTION_MOVE:
                action = "ACTION_MOVE";
                break;
            case MotionEvent.ACTION_UP:
                action = "ACTION_UP";
                break;
            default:
                return null;
        }

        return String.format("%s\n%.1f, %.1f", action, event.getX(), event.getY());
    }
}

活动的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <CheckBox
        android:id="@+id/checkbox_lock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lock Selection" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/selection_first"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="First"/>
        <RadioButton
            android:id="@+id/selection_second"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Second"/>
        <RadioButton
            android:id="@+id/selection_third"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Third"/>
    </RadioGroup>
</LinearLayout>

请注意,此代码片段来自Dave Smith名为Custom Touch Examples的项目

4

1 回答 1

2

https://developer.android.com/reference/android/view/View.OnTouchListener.html的文档中,返回truefromonTouch表示event已被使用。虽然我不是 100% 确定,但我的结论是它不应该传播到层次结构中的其他元素,因此事件不会传递到RadioButton布局中的三个 s,这意味着它们不会通知任何触摸。

onTouch方法有两个目的,允许您对该事件执行自定义逻辑,并指示是否通过返回值将事件传递给其他处理程序。

于 2016-10-17T11:43:01.857 回答