In onTouchEvent, ACTION_UP doesn't work

2019-01-26 05:24发布

I'd like to read when a player touches the screen, and when not.

    @Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP){  //ACTION UP
        actionOnUP = true;
        Log.v("MC", "Up");
    }
    if(event.getAction() == 0){ //ACTION DOWN
        actionOnUP = false;
        Log.v("MC", "Down");
    }
    Log.v("MC", event.getAction() + " ");
    return super.onTouchEvent(event);
}

This code, yes, it works, but only when player touch the screen (ACTION_DOWN), but when he don't touching the screen (ACTION_UP), nothing happens :/ LogCat

^ This is screen form LogCat. You can see: this is only ACTION_DOWN, but nothing about ACTION_UP. Class is extending View:

public class MainClass extends SurfaceView implements SurfaceHolder.Callback {

Can you help me?

EDIT: My game is based on this tutorial: http://www.droidnova.com/2d-tutorial-series-part-v,848.html

3条回答
狗以群分
2楼-- · 2019-01-26 05:50

"petey"'s solution worked for me ! Except some syntax errors, corrected here :

int code = event.getAction() & MotionEvent.ACTION_MASK;
if ((code == MotionEvent.ACTION_POINTER_UP) || (code == MotionEvent.ACTION_UP) || (code == MotionEvent.ACTION_CANCEL)) {

Thanks a lot.

查看更多
太酷不给撩
3楼-- · 2019-01-26 05:51

try :

int action = event.getAction();
int code = action & MotionEvent.ACTION_MASK;
if (code == MotionEvent.ACTION_POINTER_UP || code == MotionEvent.ACTION_UP || MotionEvent.ACTION_CANCEL) {
查看更多
Animai°情兽
4楼-- · 2019-01-26 05:58

My guess is that super.onTouchEvent is returning false, as whatever superclass you're calling doesn't care about the touch event.

If you return false to onTouchEvent, then the Android OS will no longer notify you of any further events in that gesture. If you want to continue receiving touch event information (ACTION_UP for example), then you must return true to the first ACTION_DOWN event.

查看更多
登录 后发表回答