安卓标记 : onTouch should call View#performClick when a click is detected

标签 android warnings lint

我有一个功能代码,但在上次 SDK 更新后我收到此警告:

Multiple markers at this line
- onTouch should call View#performClick when a click is detected
- implements android.view.View.OnTouchListener.onTouch

med.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            detect.setEnabled(false);
        }
        if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            detect.setEnabled(true);
        }
        //v.performClick();
        Log.e("next", "touch");
        return false;
    }
});

med.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(!center) {
            send("1");
        } else {
            send("2");
        }
        Vibrate(100);
        Log.e("next","click");
    }
});

我的代码工作正常,但如果我取消注释 v.performClick();删除警告我得到不需要的行为。为什么我会收到此警告?如果我放弃它并保持代码不变,是否会出现一些问题?

编辑:

这是我点击按钮时的日志:
“下一步”、“触摸”
“下一步”,“触摸”
“下一步”,“点击”

这是 v.performClick()
“下一步”,“点击”
“下一步”,“触摸”
“下一步”,“点击”
“下一步”,“触摸”

“下一步”、“点击”

最佳答案

你应该把

v.performClick();

在第二个 if 中:

if (event.getAction() == MotionEvent.ACTION_UP ....) {
    v.performClick();
    detect.setEnabled(false);
}

关于安卓标记 : onTouch should call View#performClick when a click is detected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25390984/

相关文章:

gradle build 中的 Android Studio Endpoints 和 peer not authenticated 错误

c++ - 可移植的UNUSED参数宏,用于C和C++的函数签名

objective-c - 为什么 gcc 会警告派生类中的 `self = [super initDesignatedInit];' 调用不兼容的结构赋值?

android - 在 Kotlin 中,我可以将 lint 应用于继承我指定的类的所有类的构造函数吗?

javascript - Vue组件脚本缩进规则冲突

android - android studio 中的菜单文件夹消失了吗?

java - Android - 实现图标包支持

android - 在数据库 SQLite 中过滤查询

c++ - Visual Studio 警告 C4996

android - 清理未使用的 Android 权限