如何检测对Android智能手表屏幕的长按?

问题描述

我基本上想检测对我的 frameLayout 的长时间触摸,一旦检测到,我将调用我在方法中拥有的警报消息。

    frameLayout.setonTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v,MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    first_tap = System.currentTimeMillis();
                    clicked = clicked + 1;
                    if (clicked == 2)
                    {
                        Toast.makeText(toolandmode.this,"Clicks:" + clicked,Toast.LENGTH_SHORT).show();
                        startTimer();
                    }
//                    else if (flag == 1)
//                    {
//                        Toast.makeText(toolandmode.this,Toast.LENGTH_SHORT).show();
//                        AlertMessage();
//                        flag = 0;
//                    }
                }

                return true;
            }
        });

我的活动包含一个计时器,我通过双击屏幕开始,而不是再次双击作为我的警报消息的触发器,我只想简单地触摸屏幕很长时间,然后我所做的警报对话框就会被触发。那我该怎么做?

解决方法

我建议您将当前的 OnTouchListener 代码替换为 GestureDetectorSimpleOnGestureListener。开箱即用,支持双击和长按。

你所要做的就是这样:

GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {

    public void onDoubleTap(MotionEvent e) {
        startTimer();
    }

    public void onLongPress(MotionEvent e) {
        alertMessage();
    }
});

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
};

有关如何将 GestureDetector 添加到视图的更多详细信息,您可以查看 this post

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...