我正在开发一个测验应用程序,我希望在测验时间结束时显示alert dialog
。所以立即时间到了,alert dialog
弹出可能是一段文字说明你的时间已到。
我确实尝试过对此进行研究,但没有任何积极的结果,所以我希望在这里得到一些积极的反馈。
下面是我为倒数计时器编写的代码。
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();
以下是我为对话编写的代码
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();
}
};
所以问题是当倒计时器启动时我如何自动呼叫或显示dialog
。
谢谢:)
答案 0 :(得分:0)
您可以在倒数计时器的onFinish()方法中显示警告对话框。当时间到了,这个方法在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();
}
}
答案 1 :(得分:0)
谢谢你们,但我能够回答修复它。显然我只需要将对话代码添加到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();