活动中的双击事件

时间:2013-01-24 16:51:53

标签: android

我有一个实现onGestureListener的活动。如何检测同一活动中的双击事件?是否可以识别活动中的双击事件?

它也不承认长按?对此有何帮助?

4 个答案:

答案 0 :(得分:6)

GestureDetector可让您指定OnDoubleTapListener以及OnGestureListener。您唯一需要做的就是实现OnDoubleTapListener并覆盖其onDoubleTap方法。

此外,您可以使用SimpleOnGestureListener并仅覆盖您想要的内容。

final Context context = this;
final GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Toast.makeText(context, "onDoubleTap", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Toast.makeText(context, "onLongPress", Toast.LENGTH_SHORT).show();
    }
};

final GestureDetector detector = new GestureDetector(listener);

detector.setOnDoubleTapListener(listener);
detector.setIsLongpressEnabled(true);

getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        return detector.onTouchEvent(event);
    }
});

答案 1 :(得分:1)

您可以在此网站上找到并回答您的问题:OnTap listener implementation

答案 2 :(得分:0)

对于任何视图,你都可以这样做

   ivPost.setOnTouchListener(new View.OnTouchListener() {
            private GestureDetector gestureDetector = new GestureDetector(ActivityName.this, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    Log.d("TEST", "onDoubleTap");
                    return super.onDoubleTap(e);
                }
                @Override
                public boolean onSingleTapConfirmed(MotionEvent event) {
                    Log.d("TEST", "onSingleTap");
                    return false;
                }
            });

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                gestureDetector.onTouchEvent(event);
                return true;
            }


        });

答案 3 :(得分:0)

onDoubleTap 函数使用特定元素(例如 ImageView id)为' selectedImage '并运行此函数名称为' selectImage ()的完整代码:

findViewById(R.id.selectdImage).setOnTouchListener(new View.OnTouchListener() {
        private GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Toast.makeText(getApplicationContext(), "onDoubleTap", Toast.LENGTH_SHORT).show();
                selectImage(nView);
                return super.onDoubleTap(e);
            }
            @Override
            public boolean onSingleTapConfirmed(MotionEvent event) {
                Log.d("onSingleTapConfirmed", "onSingleTap");
                return false;
            }
        });

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            gestureDetector.onTouchEvent(event);
            return true;
        }
    });