Android Touch UP not working

2019-07-13 11:22发布

问题:

im kinda stuck with this project in android im trying to figure out why this is not working but i have no idea.This is By the way a custom view just in case.

The problem is this the event ACTION_DOWN is working perfectly im getting the console debug "Touching 1" and "Touching 2" but ACTION_UP,ACTION_CANCEL or default: are not working i cant figure this out so any help would be really appreciated thank you

        public void doTouch(MotionEvent event)
        {
            int action = event.getAction();

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

            switch(action)
            {
            case MotionEvent.ACTION_DOWN:
                if(x < 125 && y < 125){
                    for(AnimatedSprite a:mSprites)
                    {
                        if(a.getID() == 1)
                        {
                            a.AdvanceToFrame(0);
                            touching = true;
                            System.out.println("Touching 1");
                            //touch_obj1 = true;
                        }
                    }
                }
                else if(x > getWidth()-125 && y > getHeight()-125)
                {
                    for(AnimatedSprite a:mSprites)
                    {
                        if(a.getID() == 2)
                        {
                            a.AdvanceToFrame(1);
                            System.out.println("Touching 2");
                            touching = true;
                            //touch_obj2 = true;
                        }
                    }
                }
                else
                {
                    touching = false;
                }
                break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:

                    System.out.println("Touch UP");
                    touching = false;
                    break;

                default:
                    System.out.println("NOT Touching");
                    touching = false;
                break;
            }
        }

here is where im calling the doTouch function

public boolean onTouchEvent(MotionEvent event)
        {
            thread.doTouch(event);
            return super.onTouchEvent(event);
        }

回答1:

It looks like your problem is the way you are handling touch events in onTouchEvent. You need to return true from onTouchEvent if you want to handle touch events. So, if you get an ACTION_DOWN and return false from onTouchEvent the system will not send you any more touch events related until the next ACTION_DOWN event.

Without knowing exactly what you are doing with the touch events, I can't give you a solution that will definitely work for your situation, but if you want to receive any touch events other than ACTION_DOWN you will need to return true from onTouchEvent.



回答2:

ok i got this fixed what i had to do was this

1)i changed the function so it could return a

boolean public bool doTouch(MotionEvent event)

2)of course i returned true at the end of that function

3)when calling it i did this

  public boolean onTouchEvent(MotionEvent event)
    {
        return doTouch(event);
    }