如何使我的Android应用程序测验一个计时器? [英] How to make a timer on my Android quiz application?
问题描述
我想提出一个Android应用程序测验,我想它有一个计时器。15秒。当定时器完成其移动到一个新的问题。我真的没有尝试过任何东西,因为我真的没有线索,除了choronometer,但它似乎是一个坏的解决方案......在code:
I am making an Android quiz application and I would like for it to have a timer.. 15 seconds. When the timer finishes it moves to a new question. I really did not tried anything because I really don't have a clue, except a choronometer but it seems like a bad solution... the code:
我已经加入这个$ C $下一个倒计时...你看,当它完成它去的方法generateQuestion()..现在它生成一个新的问题,但答案是混在一起......它显示的答案从一些其他的问题..我该如何解决这个问题?
I have added this code for a countdown... and you see when it finishes it goes to a method generateQuestion().. now it generates a new question but the answers are mixed up.. it shows answer from some other questions.. How do I solve this?
package com.matej.hajdukkviz;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
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);
// ADD THE QUESTIONS IN THE ArrayList qsts
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;
}
}
}
推荐答案
我不打算然而,写了code的你,在这种情况下,我想你想的 CountdownTimer 。这是非常容易使用。下面是从文档的例子。
I'm not going to write the code for you, however, in this situation I think you want a CountdownTimer. It is very easy to use. Here is the example from the docs.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
基本上, onTick()
将允许您更新的TextView
还剩秒或什么(你还可以做任何你想在这里)。那么 onFinish()
将允许你,说,去下一个活动
Basically, the onTick()
will allow you to update a TextView
with the seconds left or whatever (you also could do anything else you wanted to here). Then the onFinish()
would allow you to, say, go to the next Activity
<一个href="http://stackoverflow.com/questions/13917997/countdowntimer-trivia-game-score-android-java/13918114#13918114">Here一个答案我给,有人曾在情况下,它可以帮助的问题。
Here is an answer I gave that someone had issues with in case it helps.
这篇关于如何使我的Android应用程序测验一个计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!