失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android点击不抬起 Android小坑-OnTouchListener()事件监听长按后抬手MotionEvent.A

android点击不抬起 Android小坑-OnTouchListener()事件监听长按后抬手MotionEvent.A

时间:2023-02-28 16:18:29

相关推荐

android点击不抬起 Android小坑-OnTouchListener()事件监听长按后抬手MotionEvent.A

场景:

控件使用OnTouchListener()事件监听,正常的流程是,按下瞬间屏幕捕捉到触摸,触发MotionEvent.ACTION_DOWN事件,滑动屏幕会触发MotionEvent.ACTION_MOVE事件,手指离开屏幕会触发MotionEvent.ACTION_UP事件,这是我们所想要的事件触发流程,但是这不是绝对的.

举例:

假如我们的步骤使:按下-长按-松手,出现MotionEvent.ACTION_UP事件不触发的情况,而是触发了MotionEvent.ACTION_CANCEL事件.综上,短暂长按松手时触发流程是:MotionEvent.ACTION_DOWN-(滑动MotionEvent.ACTION_MOVE)-MotionEvent.ACTION_UP,长时间长按的触发流程:MotionEvent.ACTION_DOWN-滑动MotionEvent.ACTION_MOVE-MotionEvent.ACTION_UP),MotionEvent.ACTION_DOWN-不滑动-MotionEvent.ACTION_CANCLE.

解释:

长按的时间多久才会触发MotionEvent.ACTION_CANCLE?这个博主目前没有一个定论.所以为了避免出错,应该在代码中加入MotionEvent.ACTION_CANCLE的触发操作.

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

//按钮按下逻辑

break;

case MotionEvent.ACTION_UP:

//按钮弹起逻辑

break;

case MotionEvent.ACTION_CANCEL:

//按钮弹起逻辑

break;

}

注:这种情况只会出现在部分机型上,可能在一些机型上不服复现MotionEvent.ACTION_CANCEL的问题,目前已知出现问题的机型有:

安卓9.0,小米5,长按抬起事件是ACTION_CANCEL

安卓8.0,小米6,长按抬起事件是ACTION_CANCEL

安卓6.0,红米4,长按抬起事件是ACTION_CANCEL

安卓6.0,荣耀7,长按抬起事件是ACTION_UP

————————————————

版权声明:本文为CSDN博主「已经毕业的C先生」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。

原文链接:/cqx13763055264/article/details/86543140

android点击不抬起 Android小坑-OnTouchListener()事件监听长按后抬手MotionEvent.ACTION_MOVE不触发问题...

如果觉得《android点击不抬起 Android小坑-OnTouchListener()事件监听长按后抬手MotionEvent.A》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。