onTouchListener警告:当检测到单击时,onTouch应调用View#performClick

Trz*_*cje 92 android ontouchlistener

我创造了一个onTouchListener.不幸的是onTouch()方法throws我警告:

com/calculator/activitys/Calculator$1#onTouch should call View#performClick when a click is detected
Run Code Online (Sandbox Code Playgroud)

这是什么意思?我没有找到有关此警告的任何信息.这是完整的代码:

LinearLayout llCalculatorContent = (LinearLayout) fragmentView.findViewById(R.id.calculator_content);

llCalculatorContent.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Tools.hideKeyboard(getActivity(), getView());
        getView().clearFocus();
        return false;
    }   
});
Run Code Online (Sandbox Code Playgroud)

Sec*_*cko 110

干得好:

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //some code....
        break;
    case MotionEvent.ACTION_UP:
        v.performClick();
        break;
    default:
        break;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 不应该只在事件== MotionEvent.Action_UP时调用它,或类似的东西?另外,不会返回"false"实际调用原始的click方法,所以你应该实际返回"true"而不是? (14认同)
  • 没有什么可以解决警告. (9认同)
  • 如果我们从这个方法返回false,我们不应该被要求调用`performClick`,对吗?在这种情况下,我不明白为什么皮棉警告仍然存在 (7认同)
  • 我有类似的警告.但是我的场景有点不同,因为我在设置RecyclerView时设置了一个匿名的OnTouchListener(调用myRecyclerView.setOnTouchListener .Android Studio正在标记整个匿名类,并发出类似警告,即使我添加了v.performClick,警告仍然存在. (3认同)
  • 没有帮助,它没有解决警告 (2认同)

yoA*_*ex5 10

当检测到点击时 onTouch 应调用 View#performClick

您可以抑制此 Lint 警告

@SuppressLint("ClickableViewAccessibility")
Run Code Online (Sandbox Code Playgroud)

你应该打电话performClick()进去onTouchEvent()

@Override
public boolean onTouchEvent(MotionEvent event) {
    //Logic 
    performClick();
    return super.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)

或者

findViewById(R.id.view1).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.performClick();
        return v.onTouchEvent(event);
    }
});
Run Code Online (Sandbox Code Playgroud)

[OnTouch流程]

在这里阅读更多内容

  • 您不必重写“performClick()”。请分享您的代码 (2认同)

CSG*_*SGO 5

如果您没有使用Custom View明确覆盖的a onPerformClick,则仅按照 Secko 的答案不会删除警告。

除了他的答案之外,为了在类似android.widget.Buttonor 的类上执行相同的操作Button,您需要创建一个扩展目标视图的简单自定义视图。

例子 :

自定义视图类:

public class UselessButton extends AppCompatButton {
    public UselessButton(Context context) {
        super(context);
    }

    public UselessButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UselessButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
Run Code Online (Sandbox Code Playgroud)

XML

<stackoverflow.onEarth.UselessButton
    android:id="@+id/left"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:background="@drawable/left"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.16"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBaseline_toBaselineOf="@+id/right"
    app:layout_constraintVertical_bias="0.5" />
Run Code Online (Sandbox Code Playgroud)

爪哇

    left.setOnTouchListener((v, event) -> {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            enLeft = 1;
            enRight = 0;
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            enLeft = 0;
            v.performClick();
            return false;
        } else {
            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)

当前问题:警告已由 IDE 解决,但在真实的 Android 设备上看不到实际执行的单击操作。

编辑:修复了获取点击事件:使用View.setPressed(boolean)

down.setOnTouchListener((v, event) -> {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        enFront = 0;
        enBack = 1;
        left.setPressed(true);
        return true;
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        enBack = 0;
        v.performClick();
        v.setPressed(false);
        return false;
    } else {
        return false;
    }
Run Code Online (Sandbox Code Playgroud)