Calendar.getInstance().getTimeInMillis(); //每次调用onClick时的时间,可返回long
onclick
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;
/**
*执行代码
/
}
}
onTouch
(自写,由于onTouch抬起按下都会有监听事件,下面代码逻辑为:一次完整按下抬起过程没有时间限制,第二次按下须与上次抬起有
MIN_CLICK_DELAY_TIME 间隔 )
public static final int MIN_CLICK_DELAY_TIME = 1000;
long upTime =0;
long downTime =0;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downTime = Calendar.getInstance().getTimeInMillis();
}
if (downTime -upTime >MIN_CLICK_DELAY_TIME) {
/**
*执行代码
/
}
if(event.getAction() == MotionEvent.ACTION_UP){
upTime=Calendar.getInstance().getTimeInMillis();
}
return false;
}
如果程序需要在多个地方使用,可以自定义一个NoDoubleClickListener(NoDoubleTouchListener),继承自OnClickListener(OnTouchListener):
/代码2
public abstract class NoDoubleClickListener implements 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);
}
}
}
noDooubleButton.setOnClickListener(new NoDoubleClickListener() {
@Override
public void onNoDoubleClick(View v) {
submitOrder();//执行代码
}
});
网友评论