【问题标题】:How to listen to doubletap on a view in android? [duplicate]如何在android中收听双击视图? [复制]
【发布时间】:2012-11-23 14:20:17
【问题描述】:

我想在视图上检测doubletap,例如button,然后知道它是哪个视图。我见过this similar question,但他们说这是重复的问题似乎没有回答我的问题。

我能find 就是在活动中添加GestureDetector,然后在其中添加OnDoubleTapListener。但这只有在我点击屏幕的背景/布局时才会触发。当我(双击)button 时,它不会触发。

这是我在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;  
        }  
    });  

【问题讨论】:

  • 单击或双击此方法句柄的完美代码:)

标签: android


【解决方案1】:

您只需使用这几行代码即可实现此目的。就这么简单。

final GestureDetector gd = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){


       //here is the method for double tap


        @Override
        public boolean onDoubleTap(MotionEvent e) {

            //your action here for double tap e.g.
            //Log.d("OnDoubleTapListener", "onDoubleTap");

            return true;
        }

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

        }

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

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }


    });

//here yourView is the View on which you want to set the double tap action

yourView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return gd.onTouchEvent(event);
        }
    });

将这段代码放在要在视图上设置双击操作的 Activity 或适配器上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多