防止Android过快点击造成多次事件的几种方法

方法一:

 btnPay.setClickable(false);
  或
  btnPay.setEnabled(false);

但是如果跳转页面失败呢(这里会涉及到网络请求),又要将按钮设置为true,太麻烦了(一个大型项目有多少个按钮,这得多增加多少行代码,我们的工资又不是按照行来结算的)

方法二:

public abstract class NoDoubleClickListener implements View.OnClickListener {
    public static final int MIN_CLICK_DELAY_TIME = 1000;
    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);

}

///////////////////////////////////
btnPay.setOnClickListener(new NoDoubleClickListener() {
          @Override
          protected void onNoDoubleClick(View v) {
              startPay();
          }
      });

    }
public class NoDoubleClick {
    public static final int INTERVALS_TIME_300 = 300;

    private long lastClickTime = 0;
    private int intervalsTime = INTERVALS_TIME_300;


    public NoDoubleClick(int intervalsTime) {
        this.intervalsTime = intervalsTime;
    }

    public boolean isDoubleClick() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime > 0 && currentTime - lastClickTime < intervalsTime) {
            return true;
        } else {
            lastClickTime = currentTime;
            return false;
        }
    }

}
///////////////////////////////////
btnPay.setOnClickListener(new OnClickListener() {
          @Override
          protected void onClick(View v) {
              if(noDoubleClick.isDoubleClick()) return;
              startPay();
          }
      });

    }

方法三:

RxView.clicks(btnPay)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onCompleted() {
                       //
                    }

                    @Override
                    public void onError(Throwable e) {
                        //
                    }

                    @Override
                    public void onNext(Object o) {
                        //
                    }
                });
打赏