防止用户过快点击的工具类,多按钮同样适用

/**
 * 描述:防止用户连续点击某一按钮
 * 创建人:菜籽
 * 创建时间:2017/7/28 下午2:15
 * 备注:
 */

public class PreventFastClick {

    private Map<Integer, Long> resourceIDMap = new HashMap<>();
    private static int defaultIntervalTime = 500;

    private static PreventFastClick preventRepeatClick;

    /**
     * 获取实例,用户可指定间隔时间
     *
     * @param intervalTime
     * @return
     */
    public synchronized static PreventFastClick getInstance(int intervalTime) {
        defaultIntervalTime = intervalTime;
        if (preventRepeatClick == null) {
            preventRepeatClick = new PreventFastClick();
        }
        return preventRepeatClick;
    }

    /**
     * 获取实例,使用默认间隔时间
     *
     * @return
     */
    public synchronized static PreventFastClick getInstance() {
        if (preventRepeatClick == null) {
            preventRepeatClick = new PreventFastClick();
        }
        return preventRepeatClick;
    }

    /**
     * 告诉用户是否是过快点击
     *
     * @param resourceID
     * @return
     */
    public boolean isFastClick(int resourceID) {
        if (!resourceIDMap.containsKey(resourceID)) {
            resourceIDMap.put(resourceID, System.currentTimeMillis());
            return false;
        }

        long firstTime = resourceIDMap.get(resourceID);
        resourceIDMap.put(resourceID, System.currentTimeMillis());
        if (System.currentTimeMillis() - firstTime < defaultIntervalTime) {
            return true;
        }

        return false;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值