问题描述
我写了Android测验程序(Java编写的)。当用户回答一个问题(点击一个按钮),我想换一个消息屏幕说他们是否正确与否,其次是5秒的暂停在转移到下一个问题之前。
I am writing a quiz program for the Android (written in Java). When the user answers a question (by clicking a button), I want to flash a message on the screen saying whether they were correct or not, followed by a 5 second pause before moving on to the next question.
然而,当点击一个答案按钮时,程序将暂停,但不显示正确/不正确的消息。一旦睡眠方法完成了消息仅出现。
However, when an answer button is clicked, the program pauses, but does not display the message of correct/incorrect. The message only comes up once the sleep method has finished.
if (correct)
Answer.setText("CORRECT!");
else
Answer.setText("WRONG!");
try { Thread.sleep(5000); }
catch(InterruptedException e) {}
在暂停期间禁用
作为奖励,我想答案按钮。
As a bonus, I'd like the answer buttons to be disabled during the pause.
推荐答案
伊莫的AsyncTask
是太多这个用例。
Imo AsyncTask
is too much for this usecase.
不要使用睡眠。
相反,设置正确/不正确的消息,比做到这一点:
Don't use sleep.Instead set your correct/incorrect message and than do this:
new Handler().postDelayed(new Runnable(){
public void run()
{
goToNextScreen();
}
}
, 5000);
这篇关于暂停执行中的Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!