Android按钮onTouchListener触发外界

时间:2016-07-30 15:25:28

标签: android

当我按下按钮然后将其移到按钮的边界外时,onTouchListener继续射击。

当我按下按钮上的一根手指和按钮外的第二根手指时,同样的事情发生了:我的onTouchListener为第二根手指开火。

我该如何避免这种情况?我只想在按下按钮的界限时才开火onTouchListener

Two fingers

One finger

这是我的onTouchListener实施

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

        if (fingerDown) {
            xHistoricalFirstFinger = (int) event.getX();
            yHistoricalFirstFinger = (int) event.getY();
            xFirstClick = xHistoricalFirstFinger;
            yFirstClick = yHistoricalFirstFinger;
            //counter++;
            fingerDown = false;
        }
        x = (int) event.getX();
        y = (int) event.getY();

        Log.v("Touched", client.remoteAdress.toString());

        mouseMoveProvider.nextX = x - xHistoricalFirstFinger;
        mouseMoveProvider.nextY = y - yHistoricalFirstFinger;
        mouseMoveProvider.Ready = true;
        xHistoricalFirstFinger = x;
        yHistoricalFirstFinger = y;

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (x - xFirstClick == 0 & y - yFirstClick == 0) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            client.MakeMove();
                        } catch (IOException e) {
                            e.printStackTrace();
                            NetworkListener.HandleNetworkIsUreachableException(context);
                        }
                    }
                }).start();
            }

            fingerDown = true;

        }
        return true;
    }
};

2 个答案:

答案 0 :(得分:1)

我认为这对你有帮助:

User touch and drags out of button region

基本上你还需要检查ACTION_MOVE并查看触摸是否已离开矩形的边界。我希望有所帮助。

答案 1 :(得分:0)

使用onClickListener

onTouchListener将触发每次触摸(手指向下,手指向上......)