大家好!我是android应用程序的初学者。我想用计时器制作android测验应用程序。每个问题都有一个计时器,并在每个下一个问题中重置。有人可以教我如何用我的java活动输入倒数计时器吗?大规模谢谢你和Godbless帮助我。这是我的代码:
public class QuizHistoryActivity extends AppCompatActivity {
private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1, answerBtn2, answerBtn3;
private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
static final private int QUIZ_COUNT = 10;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData [][] = {
{"Question random", "correctanswer",
"choice a", "choice b", "choice c"},
{"Question random", "correct answer",
"choice a,""choice b","choice c"},
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_history);
countLabel = (TextView)findViewById(R.id.countlabel);
questionLabel= (TextView)findViewById(R.id.questionlabel);
answerBtn1 = (Button)findViewById(R.id.answerbtn1);
answerBtn2 = (Button)findViewById(R.id.answerbtn2);
answerBtn3 = (Button)findViewById(R.id.answerbtn3);
//Create quizArray from quizdata
for (int i = 0; i < quizData.length; i++) {
//Prepare array
ArrayList<String> tmpArray = new ArrayList<>();
tmpArray.add(quizData[i][0]);
tmpArray.add(quizData[i][1]);
tmpArray.add(quizData[i][2]);
tmpArray.add(quizData[i][3]);
tmpArray.add(quizData[i][4]);
//Add tmpArray to quizArray
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz () {
//Update quizCountLabel
countLabel.setText("Question #" + quizCount);
//Generate random number between 0 and 14 (Quiz Array's size -1)
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
//Pick ine quiz set
ArrayList<String> quiz = quizArray.get(randomNum);
//Set question and right answer
//array format
questionLabel.setText(quiz.get(0));
rightAnswer = quiz.get(1);
//remove "country" from quiz and shuffle choice
quiz.remove(0);
Collections.shuffle(quiz);
//Set Choices
answerBtn1.setText(quiz.get(0));
answerBtn2.setText(quiz.get(1));
answerBtn3.setText(quiz.get(2));
//Remove this quiz from quizArray
quizArray.remove(randomNum);
}
public void checkAnswer (View view) {
//Get pushed button
Button answerBtn = (Button)findViewById(view.getId());
String btnText = answerBtn.getText().toString();
String alertTitle;
if(btnText.equals(rightAnswer)) {
//Correct!
alertTitle = "Correct!";
rightAnswerCount++;
}else {
//Wrong
alertTitle = "Wrong";
}
//create Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(alertTitle);
builder.setMessage("Answer : \n \t \t" + rightAnswer);
builder.setPositiveButton("Got It!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (quizCount == QUIZ_COUNT) {
//Show Result
Intent resultintent = new Intent(getApplicationContext(), ResultQuizHistoryActivity.class);
resultintent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
startActivity(resultintent);
}else {
quizCount++;
showNextQuiz();
}
}
});
builder.setCancelable(false);
builder.show();
}
}
答案 0 :(得分:0)
这是它
CountDownTimer countDown;
countDown= new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
//call nextQuestionMethod here
}
};
countDown.start();
30秒计时器, 1秒勾选,您可以根据自己的选择更改这些值。有关详细信息,请参阅此link。
答案 1 :(得分:0)
您可以使用Android框架中的计时器在UI中显示时间,并且:
long start = System.currentTimeMillis();
在测验开始时和结束时
long end = System.currentTimeMillis();
long timeTranscurredInMillis = end - start;
...以获得测验持续的时间。