防止過快點擊形成屢次事件執行(防止按鈕重複點擊)

自定義一個NoDoubleClickListener,繼承自OnClickListeneride

public abstract class NoDoubleClickListener implements OnClickListener {

    //兩次點擊按鈕之間的點擊間隔不能少於2000毫秒
    public static final int MIN_CLICK_DELAY_TIME = 2000;
    private long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
            lastClickTime = currentTime;
            onNoDoubleClick(v);
        }
    }

    protected abstract void onNoDoubleClick(View v);
}

使用code

buttn.setOnClickListener(new NoDoubleClickListener() {
    @Override
    protected void onNoDoubleClick(View v) {
        //點擊事件後的邏輯事件
    }
});
相關文章
相關標籤/搜索