Avoid button multiple rapid clicks

前端 未结 20 1456
猫巷女王i
猫巷女王i 2020-11-28 02:55

I have a problem with my app that if the user clicks the button multiple times quickly, then multiple events are generated before even my dialog holding the button disappear

20条回答
  •  攒了一身酷
    2020-11-28 03:00

    So, this answer is provided by ButterKnife library.

    package butterknife.internal;
    
    import android.view.View;
    
    /**
     * A {@linkplain View.OnClickListener click listener} that debounces multiple clicks posted in the
     * same frame. A click on one button disables all buttons for that frame.
     */
    public abstract class DebouncingOnClickListener implements View.OnClickListener {
      static boolean enabled = true;
    
      private static final Runnable ENABLE_AGAIN = () -> enabled = true;
    
      @Override public final void onClick(View v) {
        if (enabled) {
          enabled = false;
          v.post(ENABLE_AGAIN);
          doClick(v);
        }
      }
    
      public abstract void doClick(View v);
    }
    

    This method handles clicks only after previous click has been handled and note that it avoids multiple clicks in a frame.

提交回复
热议问题