【发布时间】:2019-12-25 06:15:59
【问题描述】:
我已经找到了这个答案:Distinguish between long press and short press on a hardware button 但我对 OnTouchListener 很感兴趣。原因是我希望短按和释放(单击)会触发某种行为,但长按(不释放)会触发动作事件,使用户能够用手指拖动小部件。
所以我正在使用 ACTION_MOVE:
val listener = View.OnTouchListener(function = { view, motionEvent ->
// check if the user gesture upon the view is of moving action
when (motionEvent.action) {
MotionEvent.ACTION_DOWN-> {
// pressed
}
MotionEvent.ACTION_MOVE -> {
view.y = motionEvent.rawY - ((view.height / 2) + 60)
view.x = motionEvent.rawX - view.width / 2
}
MotionEvent.ACTION_UP -> {
// Released
问题是 ACTION_MOVE 会立即触发。是否只有当新闻很长时才可以调用它?我想也许可以使用时间戳来启用和禁用它,但有没有更简单的解决方案?
【问题讨论】:
标签: android kotlin ontouchlistener