有时候我们做app需要让用户点击按钮,但是不希望用户连续点击恶意破坏,就需要对button做出限制,如下:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//限制不能连续点击
if (isFastDoubleClick()) {
return;
}
Message message = new Message();
message.what = 1;
mHandler.sendEmptyMessage(message.what);
time.start();
}
});
//button在1000毫秒内不能起效
public static boolean isFastDoubleClick() { long time = System.currentTimeMillis(); long timeD = time - mlastClickTime; if (0 < timeD && timeD < 1000) { return true; } mlastClickTime = time; return false; }