如何在 AlertDialog 上自动暂停我的 CountDownTimer 并在对话框关闭后恢复它
How to automatically Pause my CountDownTimer on AlertDialog and Resume it after the dialog is closed
我有一个基本的测验应用程序。我正在设置一个警告对话框,为用户提供问题说明(无论是 right/wrong)
问题:我不明白,如何放置逻辑:在 AlertDialog 上暂停我的 CountDownTimer 并在单击关闭对话框按钮后自动恢复它。
仅供参考:我找到了关于如何为 CountDownTimer 暂停和恢复的答案,但我找不到关于以编程方式为 AlertDialog 设置它的答案。
这是我的工作代码:
new CountDownTimer(40000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Sec: " + millisUntilFinished / 1000);
}
public void onFinish() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this);
alertDialogBuilder
.setTitle("Time's Up!")
.setMessage("Your Final Score is " + mScore + " points.")
.setCancelable(false)
.setPositiveButton("REPLAY",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(getApplicationContext(), EasyLevel1.class));
finish();
}
})
.setNegativeButton("EXIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}.start();
这是我的回答逻辑:
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer1.getText() == mAnswer){
mScore+=5;
score.setText("Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this, R.style.quizAlert);
alertDialogBuilder
.setTitle("Bingo! Your answer is Correct.\n" + "Score: +5")
.setMessage("Correct Answer: " + mExplanation)
.setCancelable(true)
.setPositiveButton("Close", null);
//startActivity(new Intent(getApplicationContext(),MainActivity.class));
//finish();
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
mScore-=2;
score.setText("Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this, R.style.quizAlert);
alertDialogBuilder
.setTitle("Oops! Your answer is Wrong.\n" + "Score: -2")
.setMessage("Correct Answer: " + mExplanation)
.setCancelable(true)
.setPositiveButton("Close", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
您知道如何 pause/restart 计时器,但是您不知道将在关闭警报对话框时执行的代码放在哪里。
您已经具备使用setPositiveButton("Close", null)
执行此操作的基础。而不是 null
你会想要使用一个监听器来重启计时器。像这样:
alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do timer stuff.
}
});
有关详细信息,请参阅文档 here。
我有一个基本的测验应用程序。我正在设置一个警告对话框,为用户提供问题说明(无论是 right/wrong)
问题:我不明白,如何放置逻辑:在 AlertDialog 上暂停我的 CountDownTimer 并在单击关闭对话框按钮后自动恢复它。
仅供参考:我找到了关于如何为 CountDownTimer 暂停和恢复的答案,但我找不到关于以编程方式为 AlertDialog 设置它的答案。
这是我的工作代码:
new CountDownTimer(40000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Sec: " + millisUntilFinished / 1000);
}
public void onFinish() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this);
alertDialogBuilder
.setTitle("Time's Up!")
.setMessage("Your Final Score is " + mScore + " points.")
.setCancelable(false)
.setPositiveButton("REPLAY",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(getApplicationContext(), EasyLevel1.class));
finish();
}
})
.setNegativeButton("EXIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}.start();
这是我的回答逻辑:
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer1.getText() == mAnswer){
mScore+=5;
score.setText("Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this, R.style.quizAlert);
alertDialogBuilder
.setTitle("Bingo! Your answer is Correct.\n" + "Score: +5")
.setMessage("Correct Answer: " + mExplanation)
.setCancelable(true)
.setPositiveButton("Close", null);
//startActivity(new Intent(getApplicationContext(),MainActivity.class));
//finish();
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
mScore-=2;
score.setText("Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this, R.style.quizAlert);
alertDialogBuilder
.setTitle("Oops! Your answer is Wrong.\n" + "Score: -2")
.setMessage("Correct Answer: " + mExplanation)
.setCancelable(true)
.setPositiveButton("Close", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
您知道如何 pause/restart 计时器,但是您不知道将在关闭警报对话框时执行的代码放在哪里。
您已经具备使用setPositiveButton("Close", null)
执行此操作的基础。而不是 null
你会想要使用一个监听器来重启计时器。像这样:
alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do timer stuff.
}
});
有关详细信息,请参阅文档 here。