Android ACTION_UP does not get fired after ACTION_MOVE

别说谁变了你拦得住时间么 提交于 2019-12-24 10:47:27

问题


I'm trying to create a view programmatically, and change its background according to its click state.

I know I can do this with xml drawables, but I want to learn to program it too.

But if I press on my view, then slide my finger, the view gets stuck in pressed state. (white = 0xaaffffff background)

My code is this :

Edit:

I solved the problem. Here is the working code :

mainContainer.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {             
            // TODO Auto-generated method stub

            int action = event.getAction();
            if (action==MotionEvent.ACTION_DOWN)
            {
                v.setBackgroundColor(0xaaffffff);                   
                return false;
            }               

            if(action==MotionEvent.ACTION_MOVE)
            {                   

            }

            if(action==MotionEvent.ACTION_CANCEL)
            {                  
                v.setBackgroundColor(0x00ffffff);
            }

            if (action==MotionEvent.ACTION_UP)
            {               
                v.setBackgroundColor(0x00ffffff);                   
                return true;
            }


            return false;

        }
    });

As you see, I tried returning true wherever I want onTouch() to be called again. But apparently there is something I do not understand here.

What am I doing wrong ?

Thanks for any help.


回答1:


What about returning true in your ACTION_UP aswell?

if (action==MotionEvent.ACTION_UP)

    {               
      v.setBackgroundColor(0x00ffffff);
      return true;
    }


来源:https://stackoverflow.com/questions/19932482/android-action-up-does-not-get-fired-after-action-move

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