Android Touch UP not working

拜拜、爱过 提交于 2019-12-12 14:50:58

问题


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


来源:https://stackoverflow.com/questions/11641064/android-touch-up-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!