java Android:似乎无法正确使用 MotionEvent.ACTION_MOVE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6275575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:09:45  来源:igfitidea点击:

Android: can't seem to use MotionEvent.ACTION_MOVE correctly

javaandroidtouch-event

提问by Stijnn

I am quite new to Android programming and Java (though I have some experience with C#, so that helps).

我对 Android 编程和 Java 很陌生(尽管我对 C# 有一些经验,所以这有帮助)。

At this moment I'm goofing around with a couple of things to get to know how everything works. I've made an activity which implements onTouchListener. I've overridden onTouch and have a switch on event.getAction():

此时此刻,我正在忙着做一些事情来了解一切是如何运作的。我做了一个实现 onTouchListener 的活动。我已经覆盖了 onTouch 并且在 event.getAction() 上有一个开关:

public boolean onTouch(View v, MotionEvent event) 
{
    float x; 
    float y;    

    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN: // gets called
        {
            x = event.getX();
            y = event.getY();   
            circle c = new circle(this, x, y, 10, 0xFFFFFF);
            _main.addView(c, tapCount++);
            break;
        }
        case MotionEvent.ACTION_MOVE: // doesnt seem to do anything
        {
            x = event.getX();
            y = event.getY();
            circle c = new circle(this, x, y, 10, 0xFFFFFF);
            _main.addView(c, tapCount++);
            break;
        }
    }
    return false;
}

Where "circle" is a class which draws a circle.

其中“circle”是一个绘制圆圈的类。

What I expected to see was a trail of circles as I dragged my finger over the screen. In stead, the circle is only being drawn when I start touching.

当我将手指拖过屏幕时,我期望看到的是一串圆圈。相反,只有当我开始触摸时才会绘制圆圈。

I have compared my code to examples (for example: this blogpost by Google: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html) and I can't find my mistake.

我将我的代码与示例进行了比较(例如:Google 的这篇博文:http: //android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html),但我找不到我的错误.

EDIT: Link to the full class: http://pastebin.com/tVDQjQhu

编辑:完整课程的链接:http: //pastebin.com/tVDQjQhu

EDIT: Fixed. One has to return true in the onTouch() function. d'oh!

编辑:固定。必须在 onTouch() 函数中返回 true。哦!

回答by Saurabh Verma

I was having the same problem while using MotionEvent.ACTION_MOVE. For MotionEvent.ACTION_MOVEto work, return true instead of false.

我在使用时遇到了同样的问题MotionEvent.ACTION_MOVE。为了MotionEvent.ACTION_MOVE工作,返回 true 而不是 false。

回答by QAMAR

For me Solution was

对我来说解决方案是

return true

in the onTouch function as you should use this

在 onTouch 功能中,因为您应该使用它

public boolean onTouch(View v, MotionEvent event){
float x; 
float y;    

switch (event.getAction())
{
    case MotionEvent.ACTION_DOWN: // gets called
    {
        x = event.getX();
        y = event.getY();   
        circle c = new circle(this, x, y, 10, 0xFFFFFF);
        _main.addView(c, tapCount++);
        break;
    }
    case MotionEvent.ACTION_MOVE: // doesnt seem to do anything
    {
        x = event.getX();
        y = event.getY();
        circle c = new circle(this, x, y, 10, 0xFFFFFF);
        _main.addView(c, tapCount++);
        break;
    }
}
return true; //the problem was here

}

}

when you return false from onTouch(View v, MotionEvent event) then only MotionEvent.ACTION_DOWN will be called. so you should return true from this function

当您从 onTouch(View v, MotionEvent event) 返回 false 时,只会调用 MotionEvent.ACTION_DOWN 。所以你应该从这个函数返回 true

Hope this is helps

希望这有帮助

回答by ayinozendy

Had the same problems too, I don't know if it's a bug or not, but I managed to make it work by adding OnClickListenerto the implementation, and implementing public void onClick(View v)then also adding setOnClickListener(this)on the constructor.

也有同样的问题,我不知道这是否是一个错误,但我设法通过添加OnClickListener到实现中来使它工作,然后实现public void onClick(View v)然后添加setOnClickListener(this)到构造函数中。