不显示MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL [英] not showing MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL

查看:716
本文介绍了不显示MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,需要响应触摸事件。我想我的应用程序到我的列表项的颜色更改为自定义颜色。我写了下面的code,但只有 MotionEvent.ACTION_DOWN 部分正在工作。在LogCat中显示, ACTION_CANCEL ACTION_UP 不叫的。你能帮我明白为什么我的code是行不通的。

这是我的code ...

  view.setOnTouchListener(新OnTouchListener(){    公共布尔onTouch(视图V,MotionEvent事件){
        如果(event.getAction()== MotionEvent.ACTION_UP){
            view.setBackgroundColor(Color.rgb(1,1,1));
            Log.d(onTouch,MotionEvent.ACTION_UP);
        }
        如果(event.getAction()== MotionEvent.ACTION_DOWN){
            view.setBackgroundColor(Color.rgb(23,128,0));
            Log.d(onTouch,MotionEvent.ACTION_DOWN);
        }
        如果(event.getAction()== MotionEvent.ACTION_CANCEL){
            view.setBackgroundColor(Color.rgb(1,1,1));
            Log.d(onTouch,MotionEvent.ACTION_CANCEL);
        }
        返回false;
    }
});


解决方案

如果您返回 onTouch 方法,没有进一步的事件被传递给听众。您应该返回真正至少 event.getAction()== MotionEvent.ACTION_DOWN

的情况下,

重构您的code下面给出:

  view.setOnTouchListener(新OnTouchListener(){@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_UP){
    view.setBackgroundColor(Color.rgb(1,1,1));
    Log.d(onTouch,MotionEvent.ACTION_UP);
}
如果(event.getAction()== MotionEvent.ACTION_DOWN){
    view.setBackgroundColor(Color.rgb(23,128,0));
    Log.d(onTouch,MotionEvent.ACTION_DOWN);
    返回true;
}如果(event.getAction()== MotionEvent.ACTION_CANCEL){
    view.setBackgroundColor(Color.rgb(1,1,1));
    Log.d(onTouch,MotionEvent.ACTION_CANCEL);
}
返回false;
}
});

I am writing an Android app that needs to respond to touch events. I want my app to change the color of my list item to a custom color. I have written the following code, but only the MotionEvent.ACTION_DOWN section is working. The LogCat shows that ACTION_CANCEL and ACTION_UP aren't called at all. Could you please help me understand why my code isn't working.

This is my code...

view.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            view.setBackgroundColor(Color.rgb(1, 1, 1));
            Log.d("onTouch", "MotionEvent.ACTION_UP" );
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            view.setBackgroundColor(Color.rgb(23, 128, 0));
            Log.d("onTouch", "MotionEvent.ACTION_DOWN" );
        }
        if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            view.setBackgroundColor(Color.rgb(1, 1, 1));
            Log.d("onTouch", "MotionEvent.ACTION_CANCEL" );
        }
        return false;
    }
});

解决方案

If you return false from onTouch method, no further events get delivered to the listener. You should return true at least in case of event.getAction() == MotionEvent.ACTION_DOWN.

Refactor your code as given below:

view.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
    view.setBackgroundColor(Color.rgb(1, 1, 1));
    Log.d("onTouch", "MotionEvent.ACTION_UP" );
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
    view.setBackgroundColor(Color.rgb(23, 128, 0));
    Log.d("onTouch", "MotionEvent.ACTION_DOWN" );
    return true;
}

if (event.getAction() == MotionEvent.ACTION_CANCEL) {
    view.setBackgroundColor(Color.rgb(1, 1, 1));
    Log.d("onTouch", "MotionEvent.ACTION_CANCEL" );
}
return false;
}
});

这篇关于不显示MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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