采用:
e.getAction() == MotionEvent.ACTION_UP
和:
e.getAction() == MotionEvent.ACTION_MOVE
如果在和 ACTION_DOWN 之后有 ACTION_UP 事件,并且之间没有 ACTION_MOVE,则它是一个水龙头
更新:
private int mLastMotionEventAction = MotionEvent.ACTION_UP;
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// SOME DOWN ACTIONS ...
} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
// SOME MOVE ACTIONS ...
} else if (e.getAction() == MotionEvent.ACTION_UP) {
if (mLastMotionEventAction == MotionEvent.ACTION_DOWN){
// SOME TAP ACTIONS ...
}
}
mLastMotionEventAction = e.getAction();
return true;
}
另一种选择(用于敏感设备):
private long mLastDownEventTime = 0;
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
mLastDownEventTime = System.currentTimeMillis();
// SOME DOWN ACTIONS ...
} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
// SOME MOVE ACTIONS ...
} else if (e.getAction() == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - mLastDownEventTime < 500){
// SOME TAP ACTIONS ...
}
}
return true;
}
仅当自上次按下后已过去 200 毫秒或更短时间时,才会触发点击操作。当然,您可以更改最佳 UX 的时间。