我在minD中对我从给定的一些答案中检索到的一段代码存有疑问,这些答案用来避免用户多次单击同一按钮。有人可以向我解释此部分代码的作用和示例吗?
代码是
private long mLastClickTime = 0;
//Avoid the user clicking more than one
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
如果条件放置在每个按钮下方。setonclicklistener
我只想了解这段代码仅做的事情:)
答案 0 :(得分:0)
elapsedRealtime()和elapsedRealtimeNanos()返回自系统启动以来的时间,并包括深度睡眠。保证该时钟是单调的,即使CPU处于省电模式,该时钟也继续滴答作响,因此通用间隔定时的推荐基础也是这样。
进一步检查此方法
https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime()
答案 1 :(得分:0)
我将使用更详细的变量名称进行解释。
private long mLastClickTime = 0;
private long theUserCannotClickTime = 1000; // milliseconds. the time that must pass before the user's click become valid
long currentTime = SystemClock.elapsedRealtime(); // not necessarily the current realworld time, and it doesn't matter. You can even use System.currentTimeMillis()
long elapsedTime = currentTime - mLastClickTime; // the time that passed after the last time the user clicked the button
if (elapsedTime < theUserCannotClickTime)
return; // 1000 milliseconds hasn't passed yet. ignore the click
}
// over 1000 milliseconds has passed. do something with the click
// record the time the user last clicked the button validly
mLastClickTime = currentTime;