ACTION_MOVE后未触发Android ACTION_UP [英] Android ACTION_UP does not get fired after ACTION_MOVE

查看:834
本文介绍了ACTION_MOVE后未触发Android ACTION_UP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式创建视图,并根据其点击状态更改其背景.

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

我知道我可以使用xml drawables做到这一点,但是我也想学习编程.

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

但是,如果我按我的视图,然后滑动手指,该视图就会陷入按下状态. (白色= 0xaaffffff背景)

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

我的代码是这样:

我解决了这个问题.这是工作代码:

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;

        }
    });

如您所见,无论何时我想再次调用onTouch(),我都尝试返回true.但显然这里有些我不理解的东西.

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.

我在做什么错了?

感谢您的帮助.

推荐答案

在ACTION_UP中也返回true怎么办?

What about returning true in your ACTION_UP aswell?

if (action==MotionEvent.ACTION_UP)

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

这篇关于ACTION_MOVE后未触发Android ACTION_UP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆