gpt4 book ai didi

android - 什么是 SystemClock.elapsedRealtime - 毫秒?

转载 作者:行者123 更新时间:2023-12-04 23:53:44 29 4
gpt4 key购买 nike

我对我从给出的一些答案中检索到的一段代码有一些疑问,这些代码用于避免用户多次单击同一个按钮。有人可以向我解释这部分代码的作用和示例吗?

代码是

private long mLastClickTime = 0;

//Avoid the user clicking more than one
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();

if 条件放在每个按钮下面。setonclicklistener

我只是想明白这段代码只是做了什么:)

最佳答案

我会用更详细的变量名来解释它。

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;

关于android - 什么是 SystemClock.elapsedRealtime - 毫秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56503495/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com