简体   繁体   English

在onTouchEvent中,ACTION_UP不起作用

[英]In onTouchEvent, ACTION_UP doesn't work

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 :/ 这段代码是的,它起作用了,但是仅当玩家触摸屏幕(ACTION_DOWN)时,但他没有触摸屏幕(ACTION_UP)时,什么也没发生:/ LogCat

^ This is screen form LogCat. ^这是LogCat的屏幕形式。 You can see: this is only ACTION_DOWN, but nothing about ACTION_UP. 您会看到:这只是ACTION_DOWN,但与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 编辑:我的游戏基于此教程: http : //www.droidnova.com/2d-tutorial-series-part-v,848.html

My guess is that super.onTouchEvent is returning false , as whatever superclass you're calling doesn't care about the touch event. 我的猜测是super.onTouchEvent返回false ,因为您正在调用的任何超类都不在乎touch事件。

If you return false to onTouchEvent, then the Android OS will no longer notify you of any further events in that gesture. 如果您向onTouchEvent返回false ,则Android操作系统将不再向您通知该手势中的任何其他事件。 If you want to continue receiving touch event information ( ACTION_UP for example), then you must return true to the first ACTION_DOWN event. 如果要继续接收触摸事件信息(例如ACTION_UP ),则必须对第一个ACTION_DOWN事件返回true

"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. 非常感谢。

try : 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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