Android 如何检测视图上的双击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2640119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect doubletap on a View?
提问by Johnson Tey
Possible Duplicate:
Android - basic gesture detection
可能重复:
Android - 基本手势检测
I'm trying to have a View sensitive to double taps on an Android. So far, I learned to set up the double tap and know what place to handle the event for action:
API: android.view.GestureDetector.OnDoubleTapListener
我正在尝试对 Android 上的双击敏感的视图。到目前为止,我学会了设置双击并知道在什么地方处理事件以进行操作:
API: android.view.GestureDetector.OnDoubleTapListener
private GestureDetector mGestureDetector;
…
mGestureDetector = new GestureDetector(this);
…
mGestureDetector.setOnDoubleTapListener(new MyDoubleTapListener());
…
private class MyDoubleTapListener implements GestureDetector.OnDoubleTapListener {
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
But How do I link it to the View? This is in a class that has a few View members.
但是我如何将它链接到视图?这是在一个有几个 View 成员的类中。
I'll really appreciate you helping me connect the dots!
我真的很感激你帮我把这些点联系起来!
回答by Mike Fulton
Your view needs to implement the onTouchEvent()method, and that method needs to pass the event along to the onTouchEvent()method of the GestureDetectorobject.
您的视图需要实现onTouchEvent()方法,并且该方法需要将事件传递给GestureDetector对象的onTouchEvent()方法。
@Override
public boolean onTouchEvent(MotionEvent event)
{
Log.v(DEBUG_TAG,"OnTouchEvent !!!");
boolean result = gestureScanner.onTouchEvent(event);//return the double tap events
return result;
}