我应使用哪些 Android 事件来完成此任务?

发布于 2024-11-28 18:04:01 字数 1405 浏览 0 评论 0原文

我有一个与鼠标侦听器相关的 Java 类,我想将其转换到我的 Android 应用程序,但无法找到必要的事件。

我的 Java 应用程序使用以下方法:

  • mouseClicked
  • mousePressed
  • mouseReleased

我想做类似的事情,但不是使用单击事件,而是使用触摸事件。我遇到了 OnTouchListener 并重写了 onTouch 方法。

mousePressedmouseReleased 的替代方案是什么?


编辑 - (在 Peter 回复后更新)

以下事件是否正确:

  • ACTION_DOWN : mouseClicked
  • ACTION_MOVE : mousePressed
  • ACTION_UP : mouseReleased

编辑 - 2 示例来源

我的活动目前没有任何 OnTouchListener 因为我希望可以将所有触摸逻辑保留在我的视图中。

视图:

/*Inside my View - Is it proper to do onTouch logic here? 
                   Or should I be doing this from the Activity?*/

public class myView {

    public boolean onTouch(MotionEvent event) {

        switch(event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //draw arrow when screen is simply touched
                break;
            case MotionEvent.ACTION_MOVE:
                //Do Logic
                break;
            case MotionEvent.ACTION_UP:
                //Do Logic
                break;        

        }

    }
}

我在视图中执行逻辑的原因是因为我想直接获取一些变量,而不是创建多个额外的 get 方法。

我可以这样做吗?或者我是否必须重写 Activity 中的 onTouch 方法并在那里执行逻辑?

I have a Java class that pertains to mouse listeners that I'm wanting to convert over to my Android app but can't quite find the necessary events.

My Java app makes use of the following methods:

  • mouseClicked
  • mousePressed
  • mouseReleased

I'm wanting to do something similar however not with click events but touch events. I have come across OnTouchListener and did an override on the onTouch method.

What are the alternatives to mousePressed and mouseReleased?

Edit - (updated after Peter's response)

Are the following events correct:

  • ACTION_DOWN : mouseClicked
  • ACTION_MOVE : mousePressed
  • ACTION_UP : mouseReleased

EDIT - 2 Example Source

My Activity doesn't have any OnTouchListener at the moment because I was hoping I could keep all the touch logic in my View.

View:

/*Inside my View - Is it proper to do onTouch logic here? 
                   Or should I be doing this from the Activity?*/

public class myView {

    public boolean onTouch(MotionEvent event) {

        switch(event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //draw arrow when screen is simply touched
                break;
            case MotionEvent.ACTION_MOVE:
                //Do Logic
                break;
            case MotionEvent.ACTION_UP:
                //Do Logic
                break;        

        }

    }
}

The reason I am doing the logic in my View is because I have some variables that I would like to grab directly rather than creating multiple extra get methods.

Am I able to do it like this? Or will I have to override the onTouch method in my Activity and do the logic there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-12-05 18:04:01

所有触摸事件(向下、向上、移动、多点触控)均通过“onTouch”处理。请参阅本教程:

All touch events (down, up, move, multitouch) are handled via 'onTouch'. See this tutorial: http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775

穿越时光隧道 2024-12-05 18:04:01

如果您想注册 View 上的点击,请实现并添加 OnClickListener 到它。
当你想注册触摸事件时,你需要实现 OnTouchListener并将其添加到该View中。

If you want to register clicks on a View, implement and add an OnClickListener to it.
When you want to register touch events you need to implement the OnTouchListener and add it to that View.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文