如何听doubletap在Android的一个看法? [英] How to listen to doubletap on a view in android?

查看:111
本文介绍了如何听doubletap在Android的一个看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测doubletap在视图,例如像一个按钮,然后知道哪个查看它。我见过这类似的问题但问题他们说这是一个复制它似乎并没有回答我的问题。

我可以找到是一个GestureDetector添加到活动中,并添加OnDoubleTapListener给它。但是,如果我点击我的屏幕背景/布局时才会触发。它不会触发,当我(双)轻按一键。

这是在code,我有我的onCreate里面:

  GD =新GestureDetector(这一点,这一点);


    gd.setOnDoubleTapListener(新OnDoubleTapListener()
    {
        @覆盖
        公共布尔onDoubleTap(MotionEvent E)
        {
            Log.d(OnDoubleTapListener,onDoubleTap);
            返回false;
        }

        @覆盖
        公共布尔onDoubleTapEvent(MotionEvent E)
        {
            Log.d(OnDoubleTapListener,onDoubleTapEvent);
            //如果第二次敲击没有被释放,它被转移
            如果(e.getAction()== MotionEvent.ACTION_MOVE)
            {

            }
            否则,如果(e.getAction()== MotionEvent.ACTION_UP)//用户发布屏幕
            {

            }
            返回false;
        }

        @覆盖
        公共布尔onSingleTapConfirmed(MotionEvent E)
        {
            Log.d(OnDoubleTapListener,onSingleTapConfirmed);
            返回false;
        }
    });
 

解决方案

您必须使用GestureDetector来实现这一点,但长按建议(由UI准则)。

 公共类MyView的扩展视图{

    GestureDetector gestureDetector;

    公共MyView的(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
                //创建新的姿态检测
        gestureDetector =新GestureDetector(上下文,新GestureListener());
    }

    //跳过测量计算,绘图

        //委托事件到所述手势检测器
    @覆盖
    公共布尔的onTouchEvent(MotionEvent E){
        返回gestureDetector.onTouchEvent(E);
    }


    私有类GestureListener扩展GestureDetector.SimpleOnGestureListener {

        @覆盖
        公共布尔onDown(MotionEvent E){
            返回true;
        }
        //事件时双击出现
        @覆盖
        公共布尔onDoubleTap(MotionEvent E){
            浮X = e.getX();
            浮动Y = e.getY();

            Log.d(枪王,去敲:(+ X +,+ Y +));

            返回true;
        }
    }
    }
 

I want to detect a doubletap on a view, like for example a button, and then know which view it was. I have seen this similar question but the question they say it is a duplicate of it does not seem to answer my question.

All I can find is to add a GestureDetector to the activity, and add a OnDoubleTapListener to it. But that is only triggered if I tap on the background/layout of my screen. It is not triggered when I (double)tap a button.

This is the code I have inside my 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;  
        }  
    });  

解决方案

You have to use GestureDetector to achieve this, but long tap is recommended (by the UI Guidelines).

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

这篇关于如何听doubletap在Android的一个看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆