Android标记:当检测到点击时,onTouch应调用View#performClick

时间:2014-08-19 18:57:17

标签: android warnings lint

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

  

此行的多个标记
     - 当检测到点击时,onTouch应调用View#performClick      - 实现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();删除警告我得到了不必要的行为。为什么我会收到此警告,如果我丢弃它并将代码保留原样会出现问题?

修改

当我点击按钮时,这是我的日志:
" next"," touch"
" next"," touch"
" next","点击"

这与v.performClick()
有关 " next","点击"
" next"," touch"
" next","点击"
" next"," touch"

" next","点击"

1 个答案:

答案 0 :(得分:7)

你应该把

v.performClick();

在第二个if:

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