Android - 防止在触摸事件上滑动/拖动

时间:2014-07-06 02:35:44

标签: android user-input ontouchevent

我使用以下代码检测屏幕上的点按,但是我注意到它会响应滑动并多次激活事件处理程序中的代码,如何停止此操作?感谢。

    @Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    if (x < viewWidth/2)
    {
        xDir -= 10;

    }

    else
    {
        xDir += 10;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

你应该使用这样的东西:

            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    int x = (int)event.getX();
                    if (x < viewWidth/2) {
                       xDir -= 10;
                    }else  {
                       xDir += 10;
                    }
                } 
            }

注意:您可以使用其他MotionEvent标志,例如ACTION_DOWN ...

相关问题