简体   繁体   中英

Android ACTION_UP and ACTION_DOWN Called at the same time

I am using touch events for input. MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP are both called during a press. It should only call ACTION_UP when my finger is lifted from the surface. They are called at the same time. See the following code and logcat output.

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

Here is the logcat output:

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

The previous logcat generates one down case and two up cases when the screen is pressed once. Now I will have an extended press.

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

See the timestamps? Down and Up are called at the same time. It is called again once I actually remove my finger from tablet two seconds later. Does anyone have suggestions?

Yur forgot to add the break; at the end of each case . Do like the following.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM