2

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

它也不识别长按?有什么帮助吗?

4

3 回答 3

9

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);
    }
});
于 2013-01-24T17:20:42.660 回答
2

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

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


        });
于 2017-10-13T10:57:02.307 回答
1

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;
        }
    });
于 2019-09-09T16:56:44.397 回答