OnTouchListener的Ontouch事件获取的机器人叫了两声 [英] Ontouch event of OnTouchListener gets called twice in android

查看:117
本文介绍了OnTouchListener的Ontouch事件获取的机器人叫了两声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造中都在运行时给定的两点之间产生一条线的应用的新。
我看到的问题是, onTouch()被调用了两次,每点击我的模拟器。我知道,两个动作( ACTION_DOWN &安培; ACTION_UP )进行检查。但我想我的应用程序调用 onTouch()只有一次。请给我一些想法。这是code,我用:

  SurfaceView surfaceview =新SurfaceView(的getContext());
SurfaceHolder H = surfaceview.getHolder();
INT行动= event.getActionMasked();
同步(H){
    如果(行动== MotionEvent.ACTION_DOWN和放大器;&安培;行动= MotionEvent.ACTION_CANCEL!)//&安培;&安培;标志==真)
    {
        Log.d(的TouchView,ACTION_DOWN);
        点指针=新的点();
        pointer.x =(int)的event.getX();
        pointer.y =(int)的event.getY();
        touchPoint.add(指针);
        view.invalidate();
        Log.d(MotionEvent.ACTION_DOWN,点+指针);
        行动= MotionEvent.ACTION_CANCEL;
        标志= FALSE;
    }
    否则,如果(行动== MotionEvent.ACTION_UP和放大器;&安培;行动= MotionEvent.ACTION_CANCEL!)//&安培;&安培;标志==真)
    {
        Log.d(的TouchView,ACTION_UP);
        点指针=新的点();
        pointer.x =(int)的event.getX();
        pointer.y =(int)的event.getY();
        touchPoint.add(指针);
        view.invalidate();
        Log.d(MotionEvent.ACTION_UP,点+指针);
        行动= MotionEvent.ACTION_CANCEL;
        标志= FALSE;
    }
    否则返回false;
}
 

解决方案

touchListener将被要求每 MotionEvent.ACTION_DOWN MotionEvent.ACTION_UP MotionEvent.ACTION_MOVE 。因此,如果要执行code只有一次,即 MotionEvent.ACTION_DOWN 然后在里面

  onTouch()
 如果(动作== MotionEvent.ACTION_DOWN)
{
//你的code
}
 

I am creating an appliction in which a line gets generated between two points given at runtime.
The problem that I see is that onTouch() is called twice for every click on my simulator. I know that two actions (ACTION_DOWN & ACTION_UP) are checked. But I want my app to call onTouch() just once. Please give me some ideas. This is the code that I used:

SurfaceView surfaceview = new SurfaceView(getContext());
SurfaceHolder h = surfaceview.getHolder();
int action = event.getActionMasked();
synchronized(h) {
    if (action == MotionEvent.ACTION_DOWN && action!=MotionEvent.ACTION_CANCEL)// && flag==true)
    {
        Log.d("TouchView","ACTION_DOWN ");
        Point pointer = new Point();
        pointer.x = (int) event.getX();
        pointer.y = (int) event.getY();
        touchPoint.add(pointer);
        view.invalidate();
        Log.d("MotionEvent.ACTION_DOWN", "point: " + pointer);
        action = MotionEvent.ACTION_CANCEL;
        flag = false;
    }
    else if(action == MotionEvent.ACTION_UP && action!=MotionEvent.ACTION_CANCEL)// && flag==true)
    {
        Log.d("TouchView","ACTION_UP");
        Point pointer = new Point();
        pointer.x = (int) event.getX();
        pointer.y = (int) event.getY();
        touchPoint.add(pointer);
        view.invalidate();
        Log.d("MotionEvent.ACTION_UP", "point: " + pointer);
        action = MotionEvent.ACTION_CANCEL;
        flag = false;
    }
    else return false;
}

解决方案

touchListener will be called for every MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, and MotionEvent.ACTION_MOVE . so if you want to execute code only once , ie MotionEvent.ACTION_DOWN then inside

onTouch()
 if (action == MotionEvent.ACTION_DOWN)
{
//your code 
}

这篇关于OnTouchListener的Ontouch事件获取的机器人叫了两声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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