业务需要,选择按钮状态频繁切换进行判断然后提示:
3秒内,按钮切换超过5次,拦截切换动作
- 可以动态设置点击时间间隔
- 可以设置点击允许间隔内最多次点击次数
工具类
public class MultiQuickClick {
//保存click时间
private static List<Long> list = new ArrayList<>();
//多次点击间隔
private static final int MIN_CLICK_INTERVAL_TIME = 3000;
//时间隔内允许通过的最多点击次数
private static final int MAX_CLICK_COUNT = 5;
public static boolean isMultiClick() {
long curTime = System.currentTimeMillis();
Log.d("aaaa", "点击时间: " + curTime);
Log.d("aaaa", "list: " + list);
if (list.isEmpty()) {
list.add(curTime);
} else {
if (isValid(curTime)) {
list.add(curTime);
if (list.size() >= MAX_CLICK_COUNT+ 1) {
if (list.size() > MAX_CLICK_COUNT+ 1) {
//清除list中最小的个值
removeMin();
}
return true;
}
} else {
List<Long> removeList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Long l = list.get(i);
if (curTime - l > MIN_CLICK_INTERVAL_TIME) {
removeList.add(l);
}
}
list.removeAll(removeList);
list.add(curTime);
}
}
return false;
}
/**
移除list最小的值(距当前最远的时间)
*/
private static void removeMin() {
Long min = Collections.min(list);
list.remove(min);
}
//此次点击是否在限制时间内
private static boolean isValid(long cur) {
for (Long time : list) {
if (cur - time > MIN_CLICK_INTERVAL_TIME) {
return false;
}
}
return true;
}
}
使用
if (!MultiQuickClick.isMultiClick()) {
Log.d("aaaa", "有效: " );
} else {
Log.d("aaaa", "无效: " );
}
日志
D/aaaa: list: []
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562450492
D/aaaa: list: [1578562450273]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562450666
D/aaaa: list: [1578562450273, 1578562450492]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562450837
D/aaaa: list: [1578562450273, 1578562450492, 1578562450666]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562450996
D/aaaa: list: [1578562450273, 1578562450492, 1578562450666, 1578562450837]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562451160
D/aaaa: list: [1578562450273, 1578562450492, 1578562450666, 1578562450837, 1578562450996]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562451320
D/aaaa: list: [1578562450273, 1578562450492, 1578562450666, 1578562450837, 1578562450996, 1578562451160]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562451484
D/aaaa: list: [1578562450492, 1578562450666, 1578562450837, 1578562450996, 1578562451160, 1578562451320]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562451643
D/aaaa: list: [1578562450666, 1578562450837, 1578562450996, 1578562451160, 1578562451320, 1578562451484]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562451805
D/aaaa: list: [1578562450837, 1578562450996, 1578562451160, 1578562451320, 1578562451484, 1578562451643]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562451964
D/aaaa: list: [1578562450996, 1578562451160, 1578562451320, 1578562451484, 1578562451643, 1578562451805]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562452135
D/aaaa: list: [1578562451160, 1578562451320, 1578562451484, 1578562451643, 1578562451805, 1578562451964]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562452308
D/aaaa: list: [1578562451320, 1578562451484, 1578562451643, 1578562451805, 1578562451964, 1578562452135]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562452484
D/aaaa: list: [1578562451484, 1578562451643, 1578562451805, 1578562451964, 1578562452135, 1578562452308]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562452659
D/aaaa: list: [1578562451643, 1578562451805, 1578562451964, 1578562452135, 1578562452308, 1578562452484]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562452846
D/aaaa: list: [1578562451805, 1578562451964, 1578562452135, 1578562452308, 1578562452484, 1578562452659]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562453011
D/aaaa: list: [1578562451964, 1578562452135, 1578562452308, 1578562452484, 1578562452659, 1578562452846]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562453198
D/aaaa: list: [1578562452135, 1578562452308, 1578562452484, 1578562452659, 1578562452846, 1578562453011]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562453381
D/aaaa: list: [1578562452308, 1578562452484, 1578562452659, 1578562452846, 1578562453011, 1578562453198]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562453572
D/aaaa: list: [1578562452484, 1578562452659, 1578562452846, 1578562453011, 1578562453198, 1578562453381]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562454022
D/aaaa: list: [1578562452659, 1578562452846, 1578562453011, 1578562453198, 1578562453381, 1578562453572]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562454927
D/aaaa: list: [1578562452846, 1578562453011, 1578562453198, 1578562453381, 1578562453572, 1578562454022]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562455710
D/aaaa: list: [1578562453011, 1578562453198, 1578562453381, 1578562453572, 1578562454022, 1578562454927]
D/aaaa: 无效:
D/aaaa: 点击时间: 1578562456420
D/aaaa: list: [1578562453198, 1578562453381, 1578562453572, 1578562454022, 1578562454927, 1578562455710]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562457348
D/aaaa: list: [1578562453572, 1578562454022, 1578562454927, 1578562455710, 1578562456420]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562458049
D/aaaa: list: [1578562454927, 1578562455710, 1578562456420, 1578562457348]
D/aaaa: 有效:
D/aaaa: 点击时间: 1578562459066
D/aaaa: list: [1578562455710, 1578562456420, 1578562457348, 1578562458049]
D/aaaa: 有效: