Android:防止过快点击造成多次事件 问题

时间:2022-11-29 04:59:23
自定义一个NoDoubleClickListener,继承自OnClickListener:
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); }
在设置点击事件时用NoDoubleClickListener替换OnClickListener,并且实现方法onNoDoubleClick替换onClick即可:
btnPay.setOnClickListener(new NoDoubleClickListener() {
@Override
protected void onNoDoubleClick(View v) {
startPay();
}
}); }