问题描述
我基本上想检测对我的 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 代码替换为 GestureDetector 和 SimpleOnGestureListener。开箱即用,支持双击和长按。
你所要做的就是这样:
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。