How to prevent rapid double click on a button

后端 未结 11 1216
野的像风
野的像风 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:30

    I am doing like this it works very well.

    public abstract class OnOneOffClickListener implements View.OnClickListener {
    
       private static final long MIN_CLICK_INTERVAL=600;
    
       private long mLastClickTime;
    
       public static boolean isViewClicked = false;
    
    
       public abstract void onSingleClick(View v);
    
       @Override
       public final void onClick(View v) {
           long currentClickTime=SystemClock.uptimeMillis();
           long elapsedTime=currentClickTime-mLastClickTime;
    
           mLastClickTime=currentClickTime;
    
           if(elapsedTime<=MIN_CLICK_INTERVAL)
               return;
           if(!isViewClicked){
               isViewClicked = true;
               startTimer();
           } else {
               return;
           }
           onSingleClick(v);        
       }
        /**
         * This method delays simultaneous touch events of multiple views.
         */
        private void startTimer() {
            Handler handler = new Handler();
    
            handler.postDelayed(new Runnable() {
    
                @Override
                public void run() {
                        isViewClicked = false;
                }
            }, 600);
    
        }
    
    }
    

提交回复
热议问题