[英]How to display alertdialog when countdown timer is done android
I am developing a quiz application and i want to show an alert dialog
when the time for the quiz is up. 我正在开发测验应用程序,我想在测验时间到了时显示一个alert dialog
。 So immediately the time is up, and alert dialog
pops up with probably a text saying your time is up. 因此,时间到了, alert dialog
弹出,可能显示一条文本,说明您的时间到了。
I did tried researching about that but nothing positive came so i was hoping to get some positive feedback here. 我曾尝试过对此进行研究,但没有任何积极的结果,因此我希望在这里获得一些积极的反馈。
below is the code i wrote for my Countdown timer. 以下是我为倒数计时器编写的代码。
final CountDownTimer timer = new CountDownTimer(1800000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Time Spent " + String.format("%d min : %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
mTextField.setText("Time is up");
timerProcessing = false;
//Intent intent = new Intent(TestActivity.this, Assesment.class);
//startActivity(intent);
}
}.start();
and below is the code i wrote for the dialogue also 下面是我为对话编写的代码
private View.OnClickListener finishListener = new View.OnClickListener() {
public void onClick(View v) {
setAnswer();
//Calculate Score
int score = 0;
for(int i=0; i<correctAns.length; i++){
if ((correctAns[i] != -1) && (correctAns[i] == selected[i]))
score++;
}
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(TestActivity.this).create();
alertDialog.setTitle("Score");
alertDialog.setMessage((score) +" out of " + (QuizFunActivity.getQuesList().length()));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Retake", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
quesIndex=0;
TestActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Review", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = true;
quesIndex=0;
TestActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Quit", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
finish();
}
});
alertDialog.show();
}
};
so question is how do i call or display the dialog
automatically when the countdown timer is up. 所以问题是倒数计时器到了时,如何自动调用或显示dialog
。
thanks :) 谢谢 :)
You can show you Alert dialog in countdown timer's onFinish() method. 您可以使用倒数计时器的onFinish()方法显示“警报”对话框。 this method gets called on Last Tick when time is up.. 时间到时,此方法将在Last Tick上调用。
public void onFinish() {
mTextField.setText("Time is up");
timerProcessing = false;
//Intent intent = new Intent(TestActivity.this, Assesment.class);
//startActivity(intent);
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(TestActivity.this).create();
alertDialog.setTitle("Score");
alertDialog.setMessage((score) +" out of " + (QuizFunActivity.getQuesList().length()));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Retake", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
quesIndex=0;
TestActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Review", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = true;
quesIndex=0;
TestActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Quit", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
finish();
}
});
alertDialog.show();
}
}
Thanks guys but i was able to answer fix it. 谢谢大家,但是我能够解决它。 apparently i just had to add the dialogue code to the Countdown timers onFinish() method 显然我只需要将对话代码添加到Countdown计时器onFinish()方法中
final CountDownTimer timer = new CountDownTimer(180000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Time Spent: " + String.format("%d min : %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
mTextField.setText("Time is up");
timerProcessing = false;
setAnswer();
//Calculate Score
int score = 0;
for(int i=0; i<correctAns.length; i++){
if ((correctAns[i] != -1) && (correctAns[i] == selected[i]))
score++;
}
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(TestActivity.this).create();
alertDialog.setTitle("Score");
alertDialog.setMessage((score) +" out of " + (QuizFunActivity.getQuesList().length()));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Retake", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
quesIndex=0;
TestActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Review", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = true;
quesIndex=0;
TestActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Quit", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
finish();
}
});
alertDialog.show();
}
}.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.