How to prevent rapid double click on a button

后端 未结 11 1275
野的像风
野的像风 2021-01-01 19:12

I have looked at the answers here - Android Preventing Double Click On A Button and implemented qezt\'s solution like and I\'ve tried setEnabled(false) like so

11条回答
  •  不思量自难忘°
    2021-01-01 19:54

    I use a function like this in the listener of a button:

    public static long lastClickTime = 0;
    public static final long DOUBLE_CLICK_TIME_DELTA = 500;
    
    public static boolean isDoubleClick(){
        long clickTime = System.currentTimeMillis();
        if(clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            lastClickTime = clickTime;
            return true;
        }
        lastClickTime = clickTime;
        return false;
    }
    

提交回复
热议问题