简体   繁体   English

Android-未检测到ACTION_UP

[英]Android - ACTION_UP not detected

I am unable to figure out why the ACTION_UP event is not being fired when I move my finger away from contact in the following code. 我无法弄清为什么在下面的代码中将手指移开而不接触时,为什么未触发ACTION_UP事件。

public boolean onTouch(View v, MotionEvent event) {
        switch(event.getActionIndex()){
        case MotionEvent.ACTION_UP:
            Utils.log("Touch Up");
            break;
        case MotionEvent.ACTION_DOWN:
            Utils.log("Touch Down");
            break;
        }
        return true;
    }

You should use getActionMasked() . 您应该使用getActionMasked()

  • getAction() returns single pointer events (and deprecated multiple pointer events...) getAction()返回单个指针事件(不建议使用多个指针事件...)
  • getActionMasked() returns single and multiple pointer events (use with getActionIndex() to determine which pointer), getActionMasked()返回单个多个指针事件(与getActionIndex()一起使用以确定哪个指针),
  • getActionIndex() returns only the pointer index. getActionIndex()仅返回指针索引。

So getActionIndex() returns which finger performed a down / up action (0, 1, 2, etc.) While getAction() or getActionMasked() return the single pointer events you want ( ACTION_DOWN , ACTION_UP , etc.) 因此, getActionIndex()返回哪个手指执行了向下/向上动作(0、1、2等),而getAction()getActionMasked()返回您想要的指针事件( ACTION_DOWNACTION_UP等)。

Replace your code with mine. 用我的代码替换。

public boolean onTouch(View v, MotionEvent event) {
          //Here's the problem            
       switch(event.getAction()){
        case MotionEvent.ACTION_UP:
            Utils.log("Touch Up");
            break;
        case MotionEvent.ACTION_DOWN:
            Utils.log("Touch Down");
            break;
        }
        return true;
    }

For others having a similar problem, one of the reasons why ACTION_UP is not fired could be overly sensitive ACTION_MOVE. 对于其他有类似问题的人,未触发ACTION_UP的原因之一可能是过于敏感的ACTION_MOVE。 In this case, ACTION_UP never gets fired because system interprets touch action as ACTION_MOVE and fires it instead. 在这种情况下,ACTION_UP永远不会被触发,因为系统会将触摸动作解释为ACTION_MOVE并触发它。

A solution for that is to check when ACTION_MOVE happens if x and y offset is big enough to handle it as ACTION_MOVE, otherwise, you can threat it as ACTION_UP. 一种解决方案是检查x和y偏移量是否足够大以将其作为ACTION_MOVE处理,何时检查ACTION_MOVE,否则,您可以将其作为ACTION_UP进行威胁。 Here is SO example. 这是SO示例。

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

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