Android – 在视图上检测doubletap和tripletap

我一直在尝试构建一个可以检测双击和三击的敲击检测器.在我的努力失败后,我在网上搜索了很长时间才找到可以使用的东西,但没有运气!奇怪的是,像这样的东西的图书馆是如此稀缺.任何帮助?
最佳答案
你可以尝试这样的事情.

虽然我一般建议不要使用三重水龙头作为模式,因为它不是用户通常习惯的东西,所以除非它正确地传达给他们,否则大多数人可能永远不会知道他们可以三重点击一个视图.实际上在移动设备上进行双重录制也是如此,在该环境中进行交互并不总是直观的方式.

view.setOnTouchListener(new View.OnTouchListener() {
    Handler handler = new Handler();

    int numberOfTaps = 0;
    long lastTapTimeMs = 0;
    long touchDownMs = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchDownMs = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                handler.removeCallbacksAndMessages(null);

                if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
                    //it was not a tap

                    numberOfTaps = 0;
                    lastTapTimeMs = 0;
                    break;
                }

                if (numberOfTaps > 0 
                        && (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
                    numberOfTaps += 1;
                } else {
                    numberOfTaps = 1;
                }

                lastTapTimeMs = System.currentTimeMillis();

                if (numberOfTaps == 3) {
                    Toast.makeText(getApplicationContext(), "triple", Toast.LENGTH_SHORT).show();
                    //handle triple tap
                } else if (numberOfTaps == 2) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //handle double tap
                            Toast.makeText(getApplicationContext(), "double", Toast.LENGTH_SHORT).show();
                        }
                    }, ViewConfiguration.getDoubleTapTimeout());
                }
        }

        return true;
    }
});

转载注明原文:Android – 在视图上检测doubletap和tripletap - 代码日志