2014-01-06 103 views
2

如何检测android中的双击?我实现OnDoubleTapListener并写道:如何检测android双击?

public boolean onDoubleTapEvent(MotionEvent e) { 
     // TODO Auto-generated method stub 
     if(e.getAction() == 1){ 
      Toast.makeText(getApplicationContext(),"Double Tap", Toast.LENGTH_SHORT).show(); 
     } 
     return true; 
    } 

但它不工作。这有什么问题?

+0

尝试取出if块,只是敬酒,当您收到该事件。这会让我们知道听众是否被正确连接。 –

+0

如果阻塞并再次测试,我将其删除。尽管如此,我仍然无法看到双击Toast消息。 – IBunny

+0

@Emmanuel:Yah谢谢,我看到了给定的帖子,但我不明白给定的代码作为答案。他为此创造了新课程。我需要的是在我的活动内部进行。我没有太多的Android经验,如果这很愚蠢,我很抱歉。 – IBunny

回答

5
public class GestureDoubleTap extends GestureDetector.SimpleOnGestureListener { 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     //some logic 
     return true; 
    } 

} 

GestureDoubleTap gestureDoubleTap = new GestureDoubleTap(); 
gestureDetector = new GestureDetector(this/* context */, gestureDoubleTap); 

view.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     return gestureDetector.onTouchEvent(motionEvent); 
    } 

}); 
+0

谢谢,但有没有办法做到这一点里面的活动,而不创建单独的类作为GestureDoubleTap? – IBunny

+0

匿名类改为单独的类作为GestureDoubleTap,否则,我很抱歉,我不知道。 – venciallee

+0

这里有另一个问题如何识别特定图标?使用MotionEvent,我们可以获得X和Y坐标,但是如何使用它可以得到具有双击的确切图标? – IBunny