Android 如何在测验中暂停活动以显示正确答案?
HY, 嗯,我正在做一个Android测试,正如你从点击按钮后的代码中看到的,它会自动生成一个新的问题,但我希望它保持这种状态一秒钟,如果正确则变为绿色,如果错误则变为红色。。我不知道如何让它暂停,也许是一个sleep()方法。。谢谢大家!Android 如何在测验中暂停活动以显示正确答案?,android,Android,HY, 嗯,我正在做一个Android测试,正如你从点击按钮后的代码中看到的,它会自动生成一个新的问题,但我希望它保持这种状态一秒钟,如果正确则变为绿色,如果错误则变为红色。。我不知道如何让它暂停,也许是一个sleep()方法。。谢谢大家! public class Glavno extends Activity implements OnClickListener { int score = 0; int counter = 0; boolean ajme = true; TextVie
public class Glavno extends Activity implements OnClickListener {
int score = 0;
int counter = 0;
boolean ajme = true;
TextView textView1, textView2, textView3, countdown;
Button btn1, btn2, btn3, btn4;
ArrayList<Question> qsts = new ArrayList<Question>();
List<Integer> generated = new ArrayList<Integer>();
ArrayList<String> allAnswers = new ArrayList<String>();
Random rng = new Random();
Question nextQuestion;
Question qjedan = new Question(
"Q1",
"Correct answer - q1",
"Wrong answer 3 - q1",
"Wrong answer 3 - q1",
"Wrong answer 3 - q1"
);
Question q2 = new Question(
"Q2",
"Correct answer - q2",
"Wrong answer 3 - q2",
"Wrong answer 3 - q2",
"Wrong answer 3 - q2"
);
Question q3 = new Question(
"Q3",
"Correct answer - q3",
"Wrong answer 3 - q3",
"Wrong answer 3 - q3",
"Wrong answer 3 - q3"
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.pitanja);
new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
textView4.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
generateQuestion();
textView2.setText("VRIJEME!");
textView2.setTextColor(Color.RED);
}
}.start();
qsts.add(qjedan);
qsts.add(q2);
qsts.add(q3);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
textView3.setText("Rezultat: " + score);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
generateQuestion();
}
public void generateQuestion(){
while(ajme = true){
int nxt = rng.nextInt(3);
if (!generated.contains(nxt)){
generated.add(nxt);
nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
break;
}
}
}
@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();
if (counter == 3) {
Intent theIntent = new Intent(this, Score.class);
theIntent.putExtra("somename", score);
startActivity(theIntent);
finish(); // Added this method call
}
else if(buttonText.equals(nextQuestion.correctAnswerText)) {
counter++;
AdView ad = (AdView) findViewById(R.id.ads);
ad.loadAd(new AdRequest());
textView2.setText("TOČNO!");
textView2.setTextColor(Color.GREEN);
textView3.setText("Rezultat: " + (score += 10));
allAnswers.clear();
generateQuestion();
return;
}
else{
counter++;
AdView ad = (AdView) findViewById(R.id.ads);
ad.loadAd(new AdRequest());
textView2.setText("NETOČNO!");
textView2.setTextColor(Color.RED);
textView3.setText("Rezultat: " + (score -= 5));
allAnswers.clear();
generateQuestion();
return;
}
}
public类Glavno扩展活动实现OnClickListener{
智力得分=0;
int计数器=0;
布尔值ajme=true;
文本视图文本视图1、文本视图2、文本视图3、倒计时;
按钮btn1、btn2、btn3、btn4;
ArrayList qsts=新的ArrayList();
生成的列表=新的ArrayList();
ArrayList allAnswers=新的ArrayList();
随机rng=新随机();
下一个问题;
问题qjedan=新问题(
“Q1”,
“正确答案-q1”,
“错误答案3-q1”,
“错误答案3-q1”,
“错误答案3-q1”
);
问题q2=新问题(
“Q2”,
“正确答案-q2”,
“错误答案3-q2”,
“错误答案3-q2”,
“错误答案3-q2”
);
问题q3=新问题(
“第三季度”,
“正确答案-第三季度”,
“错误答案3-q3”,
“错误答案3-q3”,
“错误答案3-q3”
);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.pitanja);
新的倒计时(20000,1000){
公共void onTick(长毫秒未完成){
textView4.setText(“剩余秒数:+millisuntillfinished/1000”);
}
公共无效onFinish(){
generateQuestion();
textView2.setText(“VRIJEME!”);
textView2.setTextColor(Color.RED);
}
}.start();
qsts.add(qjedan);
qsts.add(q2);
qsts.add(第三季度);
textView1=(TextView)findViewById(R.id.textView1);
textView2=(TextView)findViewById(R.id.textView2);
textView3=(TextView)findViewById(R.id.textView3);
textView3.setText(“Rezultat:+score”);
btn1=(按钮)findViewById(R.id.btn1);
btn2=(按钮)findViewById(R.id.btn2);
btn3=(按钮)findViewById(R.id.btn3);
btn4=(按钮)findViewById(R.id.btn4);
btn1.setOnClickListener(此);
btn2.setOnClickListener(此);
btn3.setOnClickListener(此);
btn4.setOnClickListener(此);
generateQuestion();
}
公共无效生成请求(){
while(ajme=true){
int nxt=rng.nextInt(3);
如果(!generated.contains(nxt)){
生成.添加(nxt);
nextQuestion=qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.Erroranswer1);
allAnswers.add(nextQuestion.Erroranswer2);
allAnswers.add(nextQuestion.Erroranswer3);
收藏。洗牌(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
打破
}
}
}
@凌驾
公共void onClick(视图v){
按钮b=(按钮)v;
String buttonText=b.getText().toString();
如果(计数器==3){
Intent theIntent=新的意图(这个,Score.class);
内容putExtra(“somename”,分数);
起始触觉(内容);
finish();//添加了此方法调用
}
else如果(buttonText.equals(nextQuestion.correctAnswerText)){
计数器++;
AdView ad=(AdView)findviewbyd(R.id.ads);
ad.loadAd(新地址());
textView2.setText(“TOČNO!”);
textView2.setTextColor(Color.GREEN);
textView3.setText(“Rezultat:+(分数+=10));
allAnswers.clear();
generateQuestion();
返回;
}
否则{
计数器++;
AdView ad=(AdView)findviewbyd(R.id.ads);
ad.loadAd(新地址());
textView2.setText(“NETOČNO!”);
textView2.setTextColor(Color.RED);
textView3.setText(“Rezultat:+(分数-=5));
allAnswers.clear();
generateQuestion();
返回;
}
}
不要让UI线程阻塞/暂停,而是使用。这样,在一段时间后会调用您的操作,而不会阻塞任何内容
从您的UI线程:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// do your timed work here, e.g. switch to the next question
}
}, 1000); // 1000 milliseconds = 1 second
你没有把正确的答案标记为正确,所以我这边没有更多的答案。祝你好运,发现有人在你的问题上浪费时间。哦,对不起,兄弟。我刚刚看到了那条评论,不用担心,我标记为正确,谢谢。哦,你能不能给我一个使用这个方法的代码示例,因为我不太懂英语,因为sh不是我的主要语言。谢谢!非常感谢你花时间来帮助我,你知道怎么把正确答案的按钮变成绿色吗?@笑话