Java 中延迟程序执行的最佳实践(Android)
我正在为 Android 制作一个测验应用程序。
用户有 30 秒的时间回答尽可能多的问题。
我在问题之间的执行时间上设置了一个小间隙,以便用户可以查看他们是否正确回答了问题:
new Handler().postDelayed(new Runnable() {
public void run() { displayNextQuestion(); }
} , 1000);
在 30 秒结束时,用户应该被重定向到结果屏幕。但是,如果 30 秒的等待时间到期,则用户会暂时进入结果屏幕,然后重定向回下一个问题。一旦 30 秒过去,如何禁用该等待时间?
应该是一个简单的答案,我只是不知道如何使用线程/执行。
谢谢
I am making a quiz app for Android.
The user has 30 seconds to answer as many questions as they can.
I create a small gap in execution time between questions so the user can see if they got the question right:
new Handler().postDelayed(new Runnable() {
public void run() { displayNextQuestion(); }
} , 1000);
At the end of the 30 seconds, the user is supposed to be redirected to a results screen. However, if the 30 seconds expires during that wait time, then the user is brought to the results screen for a moment, only to be redirected back to the next question. How can I disable that moment of wait time once the 30 seconds has elapsed?
Should be a simple answer, I just don't know how to work with threads/execution.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 runnable 和处理程序保存到实例变量中,并调用 myHandler.removeCallbacks(myRunnable) 来放弃挂起的操作。
You can save your runnable and the handler to instance variables and call
myHandler.removeCallbacks(myRunnable)
to discard the pending action.只需使用布尔值来检查是否应显示结果,然后执行类似的操作
如果(!结果显示){
显示下一个问题();
}
Just use a boolean to check if the results should be displayed, and then do something like
if(!resultsDisplayed){
displayNextQuestion();
}