如何在android中添加双击按钮

时间:2012-10-16 07:32:37

标签: android

我想双击按钮事件。任何人都可以给我一个想法。谢谢

1 个答案:

答案 0 :(得分:2)

你为什么不使用长按?或者你已经在使用其他东西?长触摸优于双触的优点:

  1. 长按是UI准则中推荐的互动,双倍 触摸不是。
  2. 这是用户的期望;用户可能找不到双触动作 因为他们不会去寻找它
  3. 已在API中处理。
  4. 实施Double Touch会影响单次触摸的处理, 因为你必须等待,看看每一次触摸都会变成 在你能够处理之前双击。
  5. 如果你想要双击:你可以使用GestureDetector。

    请参阅以下代码:

    public class MyView extends View {
    
    GestureDetector gestureDetector;
    
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
                // creating new gesture detector
        gestureDetector = new GestureDetector(context, new GestureListener());
    }
    
    // skipping measure calculation and drawing
    
        // delegate the event to the gesture detector
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return gestureDetector.onTouchEvent(e);
    }
    
    
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
    
            Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
    
            return true;
        }
    }
    }