不能同时处理这两个点击和触摸事件这两个、事件

2023-09-12 09:08:11 作者:生生世世爲妳詠歌。

我想处理触摸事件,点击一个按钮的事件。我做到以下几点:

I am trying to handle touch events and click events on a button. I do the following:

button.setOnClickListener(clickListener);
button.setOnTouchListener(touchListener);

在任何一个监听器被注册的事情正常工作,但是当我尝试使用它们都只有触摸事件被触发。任何解决方法吗?我究竟做错了什么?

When any one listener is registered things work fine but when I try to use them both only the touch events are fired. Any workaround? What am I doing wrong?

推荐答案

之间有一个微妙的,但很重要的区别 ClickListener TouchListener 。该 TouchListener 执行前视​​图能够对事件做出响应。该 ClickListener 将接收其事件后,才认为已经处理了。

There is a subtle, yet very important difference between the ClickListener and the TouchListener. The TouchListener is executed before the view can respond to the event. The ClickListener will receive its event only after the view has handled it.

所以,当你触摸屏幕时, TouchListener 首先执行,当您返回为您的活动,在 ClickListener 将永远不会得到它。但是,如果你preSS设备的轨迹球,在 ClickListener 应该被解雇,因为 TouchListener 不会回应吧。

So when you touch your screen, the TouchListener is executed first and when you return true for your event, the ClickListener will never get it. But if you press the trackball of your device, the ClickListener should be fired because the TouchListener will not respond to it.