实现Android双击时出错

时间:2014-01-03 05:32:27

标签: android events ontouchlistener double-click tap

我在Activity类中实现了OnDoubleTapListener并覆盖了三个方法,如下所示。

@Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }



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



    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

但是当我在真实设备中运行它时,没有任何事情发生。错误是什么?我怎么能找到特定的录音项目(Double taped item)?

我看到了一些使用onTouchEvent(MotionEvent e)方法的教程,并计算两次触摸之间的时差。这个过程的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

试试这个:

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;
        }
    }
}

答案 1 :(得分:0)

GestureDetector将使用您已经拥有的触摸事件,例如,如果您不希望使用以下代码,我不使用ACTION_UP,因此我不需要使用该事件(只会消耗第二个水龙头)

public class DoubleTapDetector {

    public interface OnDoubleTapListener {
        boolean onDoubleTap(MotionEvent e);

        boolean onDoubleTapEvent(MotionEvent e);
    }

    private int mDoubleTapSlopSquare;

    private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout();
    private static final int DOUBLE_TAP_MIN_TIME = 40;

    private static final int DOUBLE_TAP_SLOP = 100;

    private OnDoubleTapListener mDoubleTapListener;

    private MotionEvent mCurrentDownEvent;

    public DoubleTapDetector(Context context, OnDoubleTapListener listener) {
        mDoubleTapListener = listener;
        init(context);
    }

    private void init(Context context) {
        if (mDoubleTapListener == null) {
            throw new NullPointerException("OnDoubleTapListener must not be null");
        }

        int doubleTapSlop;
        if (context == null) {
            doubleTapSlop = DOUBLE_TAP_SLOP;
        } else {
            final ViewConfiguration configuration = ViewConfiguration.get(context);
            doubleTapSlop = configuration.getScaledDoubleTapSlop();
        }
        mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop;
    }

    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();

        boolean handled = false;

        switch (action) {
        case MotionEvent.ACTION_DOWN:
            if ((mCurrentDownEvent != null) &&
                isConsideredDoubleTap(mCurrentDownEvent, ev)) {
                // This is a second tap
                // Give a callback with the first tap of the double-tap
                handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
                // Give a callback with down event of the double-tap
                handled |= mDoubleTapListener.onDoubleTapEvent(ev);
            } else {
                // This is a first tap
            }

            if (mCurrentDownEvent != null) {
                mCurrentDownEvent.recycle();
            }
            mCurrentDownEvent = MotionEvent.obtain(ev);
            break;
        }

        return handled;
    }

    private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent secondDown) {
        final long deltaTime = secondDown.getEventTime() - firstDown.getEventTime();
        if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) {
            return false;
        }

        int deltaX = (int) firstDown.getX() - (int) secondDown.getX();
        int deltaY = (int) firstDown.getY() - (int) secondDown.getY();
        return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);
    }

}

用法:

DoubleTapDetector doubleTapDetector = new DoubleTapDetector(conversationWindow, new DoubleTapDetector.OnDoubleTapListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // Double tap detected.
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }
    });

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