3 回答
TA贡献1809条经验 获得超8个赞
更好的选择是创建一个轻量级的Abstract类
public abstract class DoubleClickListener implements OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
onDoubleClick(v);
lastClickTime = 0;
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}
并像这样使用
view.setOnClickListener(new DoubleClickListener() {
@Override
public void onSingleClick(View v) {
}
@Override
public void onDoubleClick(View v) {
}
});
TA贡献1871条经验 获得超8个赞
下面的代码使用非常简单的逻辑
boolean firstTouch = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == event.ACTION_DOWN){
if(firstTouch && (Helper.getCurrentTimeInMilliSeconds() - time) <= 300) {
//do stuff here for double tap
Log.e("** DOUBLE TAP**"," second tap ");
firstTouch = false;
} else {
firstTouch = true;
time = Helper.getCurrentTimeInMilliSeconds();
Log.e("** SINGLE TAP**"," First Tap time "+time);
return false;
}
}
return true;
}
- 3 回答
- 0 关注
- 553 浏览
添加回答
举报