工具处理类
public class ClickLookUtils {
private final static Map<Integer, ClickLookUtils> MAP = new ConcurrentHashMap<>();
@IdRes
private final int viewId;
private int interval;
private ClickLookUtils(int viewId) {
this.viewId = viewId;
this.interval = 200;
}
public static ClickLookUtils createFactory(View view) {
return createFactory(view.getId());
}
public static ClickLookUtils createFactory(@IdRes int viewId) {
ClickLookUtils obj = MAP.get(viewId);
if (null == obj) {
obj = new ClickLookUtils(viewId);
}
return obj;
}
public ClickLookUtils setInterval(int interval) {
this.interval = interval;
return this;
}
public boolean checkLook() {
ClickLookUtils obj = MAP.get(viewId);
if (null == obj) {
MAP.put(viewId, this);
new Handler(Looper.myLooper()).postDelayed(() -> MAP.remove(viewId), interval);
return false;
}
return true;
}
}
使用方式
if (ClickLookUtils.createFactory(view).checkLook()) {
Log.w("TAG", "200毫秒内不可重复点击");
} else {
Log.w("TAG", "可点击");
}
|