Android ACTION_UP和ACTION_DOWN同时调用

时间:2015-09-01 19:11:03

标签: android

我正在使用触摸事件进行输入。在按下期间调用MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP。当我的手指从表面抬起时,它应该只调用ACTION_UP。他们同时被召唤。请参阅以下代码和logcat输出。

    public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mRenderer.scanButtonsDown(x, y);

            Log.i("Touch", "Action Down Case");

        case MotionEvent.ACTION_UP:
            mRenderer.scanButtonsUp(x, y);

            Log.i("Touch", "Action Up Case");

    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}

这是logcat输出:

09-01 14:58:24.801    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Down Case
09-01 14:58:24.811    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case
09-01 14:58:24.961    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case

当按下屏幕一次时,前一个logcat会生成一个down case和两个up case。现在我将有一个扩展的新闻。

09-01 14:58:37.113    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Down Case
09-01 14:58:37.123    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case
09-01 14:58:41.097    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case

查看时间戳?同时调用Down和Up。一旦我在两秒钟后将手指从平板电脑上移开,它再次被调用。有没有人有建议?

1 个答案:

答案 0 :(得分:3)

您忘记在每个break;的末尾添加case。如下所示。

switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mRenderer.scanButtonsDown(x, y);

        Log.i("Touch", "Action Down Case");
        break; //add this line
    case MotionEvent.ACTION_UP:
        mRenderer.scanButtonsUp(x, y);

        Log.i("Touch", "Action Up Case");
        break; //and this line

}