如何检测Android中的触摸事件

时间:2021-03-03 20:53:33

Is it possible to detect all touch events in an Activity and capture it and then in return pass that pass event to another View?

是否可能检测活动中的所有触摸事件并捕获它,然后将事件传递给另一个视图?

For example:

例如:

Button 1 and Button 2. When Button 1 is pressed I want to capture that touch/click event and automatically pass that touch event to Button 2, basically with one touch/press you get the click generated and that same click is passed on to the second button automatically.

按钮1和按钮2。当按下按钮1时,我想要捕获那个触摸/单击事件并自动将该触摸事件传递给按钮2,基本上只需一次触摸/单击就会生成单击,同样的单击将自动传递给第二个按钮。

2 个解决方案

#1


18  

take look this API description first.

首先看看这个API描述。


boolean android.app.Activity.dispatchTouchEvent(MotionEvent ev)

布尔android.app.Activity。dispatchTouchEvent(MotionEvent ev)

public boolean dispatchTouchEvent (MotionEvent ev) Since: API Level 1 Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.

公共布尔dispatchTouchEvent (MotionEvent ev),因为:API级别1调用来处理触摸屏事件。您可以重写此命令,在将所有触摸屏事件发送到窗口之前拦截它们。请确保为应该正常处理的触摸屏事件调用此实现。

Parameters ev The touch screen event.

参数ev触摸屏事件。

Returns boolean Return true if this event was consumed.

如果使用此事件,返回布尔返回true。

As you can see, you can intercept all touch events.

如您所见,您可以拦截所有的触摸事件。

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    super.dispatchTouchEvent(ev);
    if(btn1.onTouchEvent(ev)){
        return btn2.onTouchEvent(ev);
    }else{
        return false;
    }
}

These codes are what you are looking I think.

我想这些代码就是你要找的。

#2


3  

I imagine that you could take the TouchEvent from the button press, and make a call to the other button, passing in the TouchEvent, but I am not sure how safe that would be. (Android may bomb on you)

我想,你可以从按钮按下TouchEvent,然后调用另一个按钮,传入TouchEvent,但我不确定这有多安全。(Android可能会攻击你)

A safer solution would be to subclass Button, and use the Observer design pattern. You could register each buttons to listen for button presses of each other button, and then you would be able to safely pass the TouchEvent's between all of them.

更安全的解决方案是子类化按钮,并使用观察者设计模式。您可以注册每个按钮来监听每个按钮按下的按钮,然后您就可以安全地在所有按钮之间传递TouchEvent。

If you are unfamiliar with the Observer design pattern, here is a link: http://en.wikipedia.org/wiki/Observer_pattern

如果您不熟悉观察者设计模式,这里有一个链接:http://en.wikipedia.org/wiki/Observer_pattern

#1


18  

take look this API description first.

首先看看这个API描述。


boolean android.app.Activity.dispatchTouchEvent(MotionEvent ev)

布尔android.app.Activity。dispatchTouchEvent(MotionEvent ev)

public boolean dispatchTouchEvent (MotionEvent ev) Since: API Level 1 Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.

公共布尔dispatchTouchEvent (MotionEvent ev),因为:API级别1调用来处理触摸屏事件。您可以重写此命令,在将所有触摸屏事件发送到窗口之前拦截它们。请确保为应该正常处理的触摸屏事件调用此实现。

Parameters ev The touch screen event.

参数ev触摸屏事件。

Returns boolean Return true if this event was consumed.

如果使用此事件,返回布尔返回true。

As you can see, you can intercept all touch events.

如您所见,您可以拦截所有的触摸事件。

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    super.dispatchTouchEvent(ev);
    if(btn1.onTouchEvent(ev)){
        return btn2.onTouchEvent(ev);
    }else{
        return false;
    }
}

These codes are what you are looking I think.

我想这些代码就是你要找的。

#2


3  

I imagine that you could take the TouchEvent from the button press, and make a call to the other button, passing in the TouchEvent, but I am not sure how safe that would be. (Android may bomb on you)

我想,你可以从按钮按下TouchEvent,然后调用另一个按钮,传入TouchEvent,但我不确定这有多安全。(Android可能会攻击你)

A safer solution would be to subclass Button, and use the Observer design pattern. You could register each buttons to listen for button presses of each other button, and then you would be able to safely pass the TouchEvent's between all of them.

更安全的解决方案是子类化按钮,并使用观察者设计模式。您可以注册每个按钮来监听每个按钮按下的按钮,然后您就可以安全地在所有按钮之间传递TouchEvent。

If you are unfamiliar with the Observer design pattern, here is a link: http://en.wikipedia.org/wiki/Observer_pattern

如果您不熟悉观察者设计模式,这里有一个链接:http://en.wikipedia.org/wiki/Observer_pattern