1.第一种方式:
private static final int MIN_DELAY_TIME = 2000; // 两次点击间隔不能少于2000ms
private static long lastClickTime = System.currentTimeMillis();
public static boolean isFastClick() {
boolean flag = true;
long currentClickTime = System.currentTimeMillis();
if ((currentClickTime - lastClickTime) <= MIN_DELAY_TIME) {
flag = false;
}
lastClickTime = currentClickTime;
return flag;
}
使用如下:
if (isFastClick()){
//判断重复点击 true
}else{
//不是重复点击 false
}
第2种方式:
private long oldClickTime = 0;
long clickTime = System.currentTimeMillis();
if (clickTime - oldClickTime < 800) return;
oldClickTime = clickTime;
使用如下:
第3种方式:
private static final int MIN_DELAY_TIME = 500; // 两次点击间隔
private static long lastClickTime = 0L;
public static boolean isFastClick() {
boolean flag = true;
long currentClickTime = System.currentTimeMillis();
if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
flag = false;
}
lastClickTime = currentClickTime;
return flag;
}
使用如下:
if (isFastClick()) {
Log.d(TAG, "onClick");
return;
}