Android防止暴力点击(连续点击)


/**
 * @Auther: Hero
 * @Date: 19.11.27 15:39:55
 * @Description:
 */
public abstract class CustomClickListener  implements View.OnClickListener{
    private long mLastClickTime;
    private long timeInterval = 1000L;

    public CustomClickListener() {

    }

    public CustomClickListener(long interval) {
        this.timeInterval = interval;
    }

    @Override
    public void onClick(View v) {
        long nowTime = System.currentTimeMillis();
        
        if (nowTime - mLastClickTime > timeInterval) {
            // 点击一次
            onSingleClick();
            mLastClickTime = nowTime;
        } else {
            // 快速且连续
            onFastClick();
        }
    }

    protected abstract void onSingleClick();
    protected abstract void onFastClick();
}

使用

mButton.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
               //点击一次
            }

            @Override
            protected void onFastClick() {
                //连续点击
            }
        });

 

你可能感兴趣的:(Android防止暴力点击(连续点击))