我在这里查看了答案-Android Preventing Double Click On A Button
并实现了qezt的解决方案,就像我尝试过的setEnabled(false)一样-

doneButton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {

            // mis-clicking prevention, using threshold of 1 second
            if (SystemClock.elapsedRealtime() - doneButtonClickTime < 1000){
                return;
            }

            //store time of button click
            doneButtonClickTime = SystemClock.elapsedRealtime();

            doneButton.setEnabled(false);

            //do actual work

        }
    });

这些都不适合超快速双击。

注意-处理完成后,我没有设置doneButton.setEnabled(true)。我只是完成了该 Activity ,所以没有太早启用按钮的问题。

最佳答案

我正在这样做,效果很好。

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);

    }

}

关于android - 如何防止快速双击按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32819477/

10-09 06:14