如何在android中的一个视图中收听双引号? - 代码日志

如何在android中的一个视图中收听双引号?

我想在视图上检测一个双引号,例如一个按钮,然后知道它是哪个视图。我看到了this similar question,但是他们说这是一个重复的问题似乎没有回答我的问题。

我可以find是为活动添加GestureDetector,并向其添加一个OnDoubleTapListener。但是只有当我点击屏幕的背景/布局时才会触发。当我(双击)点击一个按钮时不会触发。

这是我的onCreate里面的代码:

    gd = new GestureDetector(this, this);


    gd.setOnDoubleTapListener(new OnDoubleTapListener()  
    {  
        @Override  
        public boolean onDoubleTap(MotionEvent e)  
        {  
            Log.d("OnDoubleTapListener", "onDoubleTap");
            return false;  
        }  

        @Override  
        public boolean onDoubleTapEvent(MotionEvent e)  
        {  
            Log.d("OnDoubleTapListener", "onDoubleTapEvent");
            //if the second tap hadn't been released and it's being moved  
            if(e.getAction() == MotionEvent.ACTION_MOVE)  
            {  

            }  
            else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen  
            {  

            }  
            return false;  
        }  

        @Override  
        public boolean onSingleTapConfirmed(MotionEvent e)  
        {  
            Log.d("OnDoubleTapListener", "onSingleTapConfirmed");
            return false;  
        }  
    });  
您必须使用GestureDetector来实现此目的,但建议使用长按(通过UI指南)。

 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;
        }
    }
    }
http://stackoverflow.com/questions/13530937/how-to-listen-to-doubletap-on-a-view-in-android

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:如何在android中的一个视图中收听双引号?