Android之防快速重复点击的全局设置

方法1:利用工具类实现,在按钮的onClick事件加入判断

private static final int MIN_DELAY_TIME= 1000;  // 两次点击间隔不能少于1000ms
private static long lastClickTime;

public static boolean isFastClick() {
      boolean flag = true;
      long currentClickTime = System.currentTimeMillis();
      if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
      }
      lastClickTime = currentClickTime;
      return flag;
}

方法2:对Activity基类进行事件拦截,在BaseActivity中重写 dispatchTouchEvent方法:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN){
        // 判断连续点击事件时间差
        if (Utils.isFastClick()){
            return true;
        }
    }
    return super.dispatchTouchEvent(ev);
}

 

总结:推荐方法2

 

 

 

你可能感兴趣的:(Android)