如何模拟长按触摸事件?或者我们如何计算触摸屏幕的时间,所有操作都处于关闭状态?

最佳答案

我终于实现了一个触摸屏长点击,全部:

textView.setOnTouchListener(new View.OnTouchListener() {

    private static final int MIN_CLICK_DURATION = 1000;
    private long startClickTime;

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

        switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            longClickActive = false;
            break;
        case MotionEvent.ACTION_DOWN:
            if (longClickActive == false) {
                longClickActive = true;
                startClickTime = Calendar.getInstance().getTimeInMillis();
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (longClickActive == true) {
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                if (clickDuration >= MIN_CLICK_DURATION) {
                    Toast.makeText(MainActivity.this, "LONG PRESSED!",Toast.LENGTH_SHORT).show();
                    longClickActive = false;
                }
            }
            break;
        }
        return true;
    }
});

其中private boolean longClickActive = false;是一个类变量。

10-04 17:10