Android 中的连续触摸事件

发布于 2024-11-28 16:32:11 字数 475 浏览 2 评论 0原文

我的一项活动有 4 个按钮。我已经为所有按钮设置了触摸监听器。

Button1.setOnTouchListener(this);
Button2.setOnTouchListener(this);
Button3.setOnTouchListener(this);
Button4.setOnTouchListener(this);

我想做的就是当我在按钮上移动手指时获取 MotionEvent.ACTION_DOWNMotionEvent.ACTION_UP

当我触摸各个按钮时,我收到这些事件,但当我将手指从一个按钮移动到另一个按钮时则不会。

在这种情况下,第一个按钮应该收到 ACTION_UP 消息,下一个按钮应该收到 ACTION_DOWN 消息。

请帮忙。

I have 4 buttons on an activity. I have set Touch Listeners for all the buttons.

Button1.setOnTouchListener(this);
Button2.setOnTouchListener(this);
Button3.setOnTouchListener(this);
Button4.setOnTouchListener(this);

All I want to do is to get MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP when I am moving my finger over the buttons.

I am getting these events when I am touching INDIVIDUAL buttons, but not when I move my finger from one button to another.

In this case, first button should get ACTION_UP message and the next button should get ACTION_DOWN message.

Kindly help.

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

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

发布评论

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

评论(3

地狱即天堂 2024-12-05 16:32:11

touchevents的这种行为是有一定逻辑的。

我认为对您来说最好的决定是将 OnTouchListener 设置为 Button1、Button2 等的父视图或重写其 OnTouchEvent() 方法。之后,您将能够在必要时手动将 MotionEvent 发送到子视图(您的案例中的按钮)。

There is a certain logic in such behavior of touchevents.

I think the best decision for you will be to set OnTouchListener to parent view of Button1, Button2, etc or override its OnTouchEvent() method. After that you will be able to send MotionEvent to child views (buttons in your case) manually when it's necessary.

就像说晚安 2024-12-05 16:32:11

我最初的直觉是,您将所有触摸事件发送到同一函数,从而导致某种类型的瓶颈。

如果您将触摸处理程序分成四个单独的函数,就像这样 - 这样,所有按钮都有独立的触摸处理程序,并且让它们都使用相同的函数来处理它们的集体事件,从而不会发生冲突。为每个按钮创建一个单独的“onTouchEventListener”,并使其看起来像这样:

Button1.setOnTouchListener(button1Listener);
Button2.setOnTouchListener(button2Listener);
Button3.setOnTouchListener(button3Listener);
Button4.setOnTouchListener(button4Listener);

以下是 Android 文档中的操作方法:

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
       // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
   ...
}

My initial instinct is that you are sending all of the touch events to the same function, causing some type of bottleneck.

What if you seperate the touch handlers into four seperate functions like this - that way, all buttons have independent touch handlers and there is no collission by having them all using the same function to handle their collective events. Make a seperate "onTouchEventListener" for each button and have it look like this:

Button1.setOnTouchListener(button1Listener);
Button2.setOnTouchListener(button2Listener);
Button3.setOnTouchListener(button3Listener);
Button4.setOnTouchListener(button4Listener);

Here is how to do it from the Android docs:

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
       // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
   ...
}
若水微香 2024-12-05 16:32:11

由于您尝试捕获涉及所有按钮(而不是仅一个按钮)的 MotionEvent,因此您必须捕获父视图中的 MotionEvent,然后以 MotionEvent.ACTION_DOWN 后接 MotionEvent 的形式将它们分派给子按钮.ACTION_UP。

我做了类似的事情,用包含许多按钮的RelativeLayout制作多点触控键盘。我基本上重写了dispatchTouchEvent并跟踪每个手指,将ACITON_DOWN和ACTION_UP代替ACTION_POINTER_DOWN和ACTION_POINTER_UP发送给孩子的辅助手指。

查看ViewGroup 的源代码,看看它如何向其子级分派事件,并调整行为以检查手指何时伸出子按钮边界,如果是这样,请在旧按钮上发送 ACTION_UP,然后在新按钮上发送 ACTION_DOWN。

Since you're trying to capture a MotionEvent that involves all of the buttons, as opposed to just one, you will have to capture the MotionEvents in the parent view, then dispatch them to children buttons in the form of MotionEvent.ACTION_DOWN followed by MotionEvent.ACTION_UP.

I did something similar to make a multitouch keyboard out of a RelativeLayout containing many buttons. I basically overrided dispatchTouchEvent and kept track of each finger, sending ACITON_DOWN and ACTION_UP in place of ACTION_POINTER_DOWN and ACTION_POINTER_UP for secondary fingers to children.

It may be useful to look at the source of ViewGroup and see how it dispatches events to its children, and tweak the behavior to check when the finger goes out of child button bounds, and if so, send an ACTION_UP on the old button then an ACTION_DOWN on the new button.

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