如何检测Android中对话框内按钮上的手势

时间:2017-03-08 17:12:08

标签: android android-dialog ontouchevent gesturedetector onfling

我试图扩展GestureDetector.SimpleOnGestureListener类以检测Dialog上的手势,它对我有用,但它没有检测到任何按钮上的单击,向下,滚动等事件,复选框,对话框中的单选按钮,它只触摸对话框内的白色区域时检测这些事件。任何人都知道如何在对话框中检测各种按钮和其他控件上的手势?

以下是我的对话框和手势监听器的代码片段:

public class SimpleDialog extends Dialog {

    private GestureDetector gestureDetector;

    public SimpleDialog(Context context) {
        super(context);
        gestureDetector = new GestureDetector(context, new MyGestureDetector());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_simple);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            System.out.println( "touch event onFling()");
            return false;
        }

        MyGestureDetector() {
            super();
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            System.out.print("touch event onSingleTapUp()");
            return super.onSingleTapUp(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            System.out.println("touch event onScroll()");
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public void onShowPress(MotionEvent e) {
            super.onShowPress(e);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            System.out.println("touch event onDown()");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return super.onDoubleTap(e);
        }

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

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onContextClick(MotionEvent e) {
            return super.onContextClick(e);
        }
    }
}

0 个答案:

没有答案