简体   繁体   English

如何检测onLongClick ACTION_UP事件?

[英]How to detect onLongClick ACTION_UP event?

I have an image which implements both OnClickListener and OnLongClickListener . 我有一个同时实现OnClickListenerOnLongClickListener的图像。 In the OnLongClickListener at press down ( ACTION_DOWN ) I start a timer. 在按下( ACTION_DOWN )的OnLongClickListener ,我启动一个计时器。 I want to cancel it at ACTION_UP . 我想在ACTION_UP取消它。

Now, if I implement OnTouchListener for this image, then I catch ACTION_DOWN for normal clicks. 现在,如果我为该图像实现OnTouchListener ,那么我将获得ACTION_DOWN的常规点击。 And I want to catch Long clicks ONLY. 而且我只想吸引长点击。

Why does not long click fire ACTION_UP event? 为什么长按不触发ACTION_UP事件? How can I detect this event and stop the timer. 如何检测此事件并停止计时器。

If you need a code, I can post it, but I do not think it will help you as there is no errors in the code. 如果您需要一个代码,我可以发布它,但是由于代码中没有错误,我认为它不会对您有帮助。

PS. PS。 I cannot stop the timer at any specific time or value as this long press continuously increase the value of another element. 我不能在任何特定的时间或值停止计时器,因为长按会不断增加另一个元素的值。 This increase process should stop only at image long press ACTION_UP . 长按ACTION_UP此增加过程应停止。

Just use OnTouchListener and forget about OnLongClickListener. 只需使用OnTouchListener,而无需再理会OnLongClickListener。 You can detect a long click by checking coordinates and pressed time, you have all that info in MotionEvent object passed by OnTouch(). 您可以通过检查坐标和按下时间来检测长时间单击,并通过OnTouch()传递MotionEvent对象中的所有信息。

1- To check coordinates, use getX() and getY() from MotionEvent and getDrawingRect() from your ImageView container. 1-要检查坐标,请使用MotionEvent中的getX()和getY()以及ImageView容器中的getDrawingRect()。

2- To check for pressed time, use getEventTime(), storing first time in ACTION_DOWN and then compute by substracting with time in ACTION_UP event. 2-要检查按下时间,请使用getEventTime(),将第一次存储在ACTION_DOWN中,然后通过在ACTION_UP事件中减去时间来进行计算。 You could assume like 800mS for a long click. 您可以假设长按一下就可以达到800mS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM