Java 在应用程序中添加倒计时计时器
我做了一个测验申请,其中包括5个问题。我制作了一个ResultActivity页面,显示测验结果 现在我想为每个问题添加一个20秒的倒计时。当倒计时结束时,它会自动转到下一个问题。问题完成后,应移至ResultActivity页面以显示结果 我不知道怎么在里面加倒计时 这是我的代码:Java 在应用程序中添加倒计时计时器,java,android,Java,Android,我做了一个测验申请,其中包括5个问题。我制作了一个ResultActivity页面,显示测验结果 现在我想为每个问题添加一个20秒的倒计时。当倒计时结束时,它会自动转到下一个问题。问题完成后,应移至ResultActivity页面以显示结果 我不知道怎么在里面加倒计时 这是我的代码: QuizActivity.java package com.example.triviality; import java.util.LinkedHashMap; import java.util.List;
QuizActivity.java
package com.example.triviality;
import java.util.LinkedHashMap;
import java.util.List;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends Activity {
List<Question> quesList;
public static int score,correct,wrong,wronganswers;
public boolean isCorrect;
static int qid=0;
int totalCount=5;
Question currentQ;
TextView txtQuestion;
RadioGroup radioGroup1;
RadioButton rda, rdb, rdc;
Button butNext;
TextView rt;
boolean nextFlag =false;
boolean isTimerFinished = false;
static LinkedHashMap lhm = new LinkedHashMap();
MyCountDownTimer countDownTimer = new MyCountDownTimer(10000 /*20 Sec*/, 1000);
//final MyCountDownTimer timer = new MyCountDownTimer(20000,1000);
public static String[] Correctanswers = new String[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.button1);
//radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
setQuestionView();
// timer.start();
rt = (TextView)findViewById(R.id.rt);
rt.setText("20");
countDownTimer.start();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
countDownTimer.cancel();
if(getNextQuestion(false)){
//Start The timer again
countDownTimer.start();
}}
});
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
public void onFinish() {
Log.e("Times up","Times up");
countDownTimer.cancel();
if (getNextQuestion(false)) {
//Start The timer again
countDownTimer.start();
}
}
@Override
public void onTick(long millisUntilFinished) {
rt.setText((millisUntilFinished/1000)+"");
Log.e("Second Gone","Another Second Gone");
Log.e("Time Remaining","seconds remaining: " + millisUntilFinished / 1000);
}
}
boolean getNextQuestion(boolean c){
nextFlag = true;
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
//grp.clearCheck();
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){
qid++;
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
grp.clearCheck();
//wronganswers=
if(!c && currentQ.getANSWER().equals(answer.getText())){
correct++;
}else{
lhm.put(currentQ.getQUESTION(),currentQ.getANSWER());
wrong++;
}
if(qid<5){
currentQ=quesList.get(qid);
setQuestionView();
}else{
score=correct;
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
return false;
}
}else{
Toast.makeText(getApplicationContext(),
"Please select atleast one Option",Toast.LENGTH_SHORT).show();
}
return true;
}
}
你可以用这个
安排一个倒计时,直到将来某个时间,并定期通知沿途的时间间隔。在文本字段中显示30秒倒计时的示例:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
对onTick(long)的调用与此对象同步,以便在上一次回调完成之前不会发生对onTick(long)的调用。这仅在onTick(long)的实现需要比倒计时间隔更长的执行时间时才相关
需要更多帮助
我希望这对你有帮助
使MyCountDownTimer倒计时
成为全局变量
// Global Variable
MyCountDownTimer countDownTimer;
boolean isTimerFinished = false;
// Oncreate
countDownTimer = new MyCountDownTimer(20000 /*20 Sec*/, 1000);
countDownTimer.start();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rda.isChecked() || rdb.isChecked() || rdc.isChecked()) {
qid++;
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
grp.clearCheck();
// wronganswers=
if (currentQ.getANSWER().equals(answer.getText())) {
correct++;
} else {
lhm.put(currentQ.getQUESTION(), currentQ.getANSWER());
wrong++;
}
getNext();
}else{
Toast.makeText(getApplicationText(), "Please Select an Option", Toast.LENGTH_LONG).show();
}
}
});
// Counter Class
@SuppressLint("NewApi")
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
Log.e("Times up","Times up");
isTimerFinished = true;
getNextQuestion();
}
@Override
public void onTick(long millisUntilFinished) {
Log.e("Second Gone","Another Second Gone");
Log.e("Time Remaining","seconds remaining: " + millisUntilFinished / 1000);
}
}
void getNextQuestion() {
nextFlag = true;
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
// grp.clearCheck();
RadioButton answer = (RadioButton) findViewById(grp
.getCheckedRadioButtonId());
if (isTimerFinished) {
// Checking if some option is selected on time finished
someName();
} else {
// No Option Selected After Time Finished
// Write your Logic here About Question Handling on "No OPTION SELECTED After Timer Finished..." My Logic May Be Wrong depends on how you are handling it.
lhm.put(currentQ.getQUESTION(), currentQ.getANSWER());
wrong++;
}
getNext();
}
void someName() {
if (rda.isChecked() || rdb.isChecked() || rdc.isChecked()) {
qid++;
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
grp.clearCheck();
if (currentQ.getANSWER().equals(answer.getText())) {
correct++;
} else {
lhm.put(currentQ.getQUESTION(), currentQ.getANSWER());
wrong++;
}
}
}
void getNext() {
if (qid < 5) {
currentQ = quesList.get(qid);
setQuestionView();
} else {
score = correct;
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next Intent
startActivity(intent);
}
}
private void setQuestionView() {
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
// Start The timer again
countDownTimer.start();
}
//全局变量
MyCountDownTimer倒计时;
布尔值=false;
//一次创建
倒计时=新的MyCountDownTimer(20000/*20秒*/,1000);
countDownTimer.start();
但是next.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(rda.isChecked()| | rdb.isChecked()| | rdc.isChecked()){
qid++;
Log.d(“yourans”,currentQ.getANSWER()+“”+answer.getText());
grp.clearCheck();
//错误答案=
if(currentQ.getANSWER().equals(answer.getText())){
正确的++;
}否则{
put(currentQ.getQUESTION(),currentQ.getANSWER());
错误++;
}
getNext();
}否则{
Toast.makeText(getApplicationText(),“请选择一个选项”,Toast.LENGTH_LONG.show();
}
}
});
//柜台
@SuppressLint(“新API”)
公共类MyCountDownTimer扩展了CountDownTimer{
公共MyCountDownTimer(长启动时间、长间隔){
超级(开始时间、间隔);
}
@凌驾
公共无效onFinish(){
Log.e(“Times up”、“Times up”);
isTimerFinished=true;
getNextQuestion();
}
@凌驾
公共void onTick(长毫秒未完成){
Log.e(“第二次消失”、“第二次消失”);
Log.e(“剩余时间”,“剩余秒数:”+millisuntillfinished/1000);
}
}
void getNextQuestion(){
nextFlag=true;
放射组grp=(放射组)findViewById(R.id.RadioGroup 1);
//grp.clearCheck();
RadioButton回答=(RadioButton)findViewById(grp
.getCheckedRadioButtonId());
如果(已完成){
//检查是否按时完成某些选项的选择
someName();
}否则{
//时间结束后未选择任何选项
//在这里写下关于“计时器完成后未选择选项…”问题处理的逻辑。我的逻辑可能是错误的,这取决于您如何处理它。
put(currentQ.getQUESTION(),currentQ.getANSWER());
错误++;
}
getNext();
}
void someName(){
if(rda.isChecked()| | rdb.isChecked()| | rdc.isChecked()){
qid++;
Log.d(“yourans”,currentQ.getANSWER()+“”+answer.getText());
grp.clearCheck();
if(currentQ.getANSWER().equals(answer.getText())){
正确的++;
}否则{
put(currentQ.getQUESTION(),currentQ.getANSWER());
错误++;
}
}
}
void getNext(){
如果(qid<5){
currentQ=quesList.get(qid);
setQuestionView();
}否则{
分数=正确;
意向意向=新意向(QuizActivity.this、ResultActivity.class);
Bundle b=新Bundle();
b、 putInt(“分数”,分数);//你的分数
意图。附加动作(b);//把你的分数放在下一个意图上
星触觉(意向);
}
}
私有void setQuestionView(){
setText(currentQ.getQUESTION());
setText(currentQ.getOPTA());
setText(currentQ.getOPTB());
setText(currentQ.getOPTC());
//再次启动计时器
countDownTimer.start();
}
您尝试过什么?如果您需要一些免费提供的代码,它在这里将不起作用。thanx寻求帮助。但有一个问题。当应用程序启动时,计时器正在启动。但它没有显示类似20 19 18…0的计时器。当计时器完成后移到下一个问题时,它不会再次从20开始。是的,现在计数器正在自动启动。但在完成最后一个问题后,计时器仍在启动。我如何显示一个流,如20 19 18…直到0。检查此场景:-如果用户未单击任何答案且时间已结束,则它是否正在获取下一个问题???我想不是。告诉我。然后我将更新答案。不,它不会移动到下一个问题。它显示Toast消息,并且计数器将在同一个问题上再次启动。假设用户没有单击任何答案并且计时器完成,我是否可以移动到下一个问题,并将该问题的答案计算为错误。?在私有空间中,设置问题视图().m getting countDownTimer无法解决错误..如果我注释countDownTimer.start();应用程序正在运行,但倒计时计时器仅在第一个问题上启动。如果未选择任何选项,则不会进入下一个问题。您好。我的应用程序几乎完成。只有两个问题。您能帮我解决吗?1)如果用户未选择任何选项,计时器完成,则应在将遗漏的问题答案视为错误。2) 当我点击“下一步”按钮而不选择任何选项时,它会向我显示
// Global Variable
MyCountDownTimer countDownTimer;
boolean isTimerFinished = false;
// Oncreate
countDownTimer = new MyCountDownTimer(20000 /*20 Sec*/, 1000);
countDownTimer.start();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rda.isChecked() || rdb.isChecked() || rdc.isChecked()) {
qid++;
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
grp.clearCheck();
// wronganswers=
if (currentQ.getANSWER().equals(answer.getText())) {
correct++;
} else {
lhm.put(currentQ.getQUESTION(), currentQ.getANSWER());
wrong++;
}
getNext();
}else{
Toast.makeText(getApplicationText(), "Please Select an Option", Toast.LENGTH_LONG).show();
}
}
});
// Counter Class
@SuppressLint("NewApi")
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
Log.e("Times up","Times up");
isTimerFinished = true;
getNextQuestion();
}
@Override
public void onTick(long millisUntilFinished) {
Log.e("Second Gone","Another Second Gone");
Log.e("Time Remaining","seconds remaining: " + millisUntilFinished / 1000);
}
}
void getNextQuestion() {
nextFlag = true;
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
// grp.clearCheck();
RadioButton answer = (RadioButton) findViewById(grp
.getCheckedRadioButtonId());
if (isTimerFinished) {
// Checking if some option is selected on time finished
someName();
} else {
// No Option Selected After Time Finished
// Write your Logic here About Question Handling on "No OPTION SELECTED After Timer Finished..." My Logic May Be Wrong depends on how you are handling it.
lhm.put(currentQ.getQUESTION(), currentQ.getANSWER());
wrong++;
}
getNext();
}
void someName() {
if (rda.isChecked() || rdb.isChecked() || rdc.isChecked()) {
qid++;
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
grp.clearCheck();
if (currentQ.getANSWER().equals(answer.getText())) {
correct++;
} else {
lhm.put(currentQ.getQUESTION(), currentQ.getANSWER());
wrong++;
}
}
}
void getNext() {
if (qid < 5) {
currentQ = quesList.get(qid);
setQuestionView();
} else {
score = correct;
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next Intent
startActivity(intent);
}
}
private void setQuestionView() {
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
// Start The timer again
countDownTimer.start();
}