在我的应用程序中,我在同一个班级中有倒数计时器和对话框。当有人按下退出按钮对话框打开并在其中 2 个按钮是和否。我想要当有人按下退出按钮时计时器暂停,如果有人按下没有按钮,它会在剩余的几秒内恢复。我知道为此我必须完成这个计时器并用剩余的秒数创建新的计时器。但我无法获得剩余的秒数。如果有人知道如何做到这一点,请帮助我。
倒数计时器代码-
counterTimer = new CountDownTimer(15000, 1000) {
public void onFinish() {
if(currentGame.getRound()==20)
{
nextBtn1.setEnabled(false);
nextBtn2.setEnabled(false);
nextBtn3.setEnabled(false);
nextBtn4.setEnabled(false);
nextBtn5.setEnabled(false);
final Handler handle = new Handler();
Toast.makeText(QuestionActivity.this, "Time's Up", Toast.LENGTH_SHORT).show();
Runnable delay = new Runnable() {
public void run() {
System.exit(0);
}
};
handle.postDelayed(delay,3000);
}
else if(currentGame.getRound()==0)
{
currentGame.decrementScore();
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
processScreen();
}
};
handle.postDelayed(delay,3000);
}
else if(currentGame.getRound()<=19)
{
nextBtn1.setEnabled(false);
nextBtn2.setEnabled(false);
nextBtn3.setEnabled(false);
nextBtn4.setEnabled(false);
nextBtn5.setEnabled(false);
currentGame.decrementScore();
final Handler handle = new Handler();
Toast.makeText(QuestionActivity.this, "Time's Up", Toast.LENGTH_SHORT).show();
Runnable delay = new Runnable() {
public void run() {
processScreen();
}
};
handle.postDelayed(delay,3000);
}
}
public void onTick(long millisUntilFinished) {
TextView time = (TextView) findViewById(R.id.timers);
time.setText( ""+millisUntilFinished/1000);
}
};
counterTimer.start();
}
对话框代码-
if(arg0.getId()==R.id.quit)
{
Button yes, no;
final Dialog dialog = new Dialog(this, R.style.FullHeightDialog);
dialog.setContentView(R.layout.dialog1);
dialog.setCancelable(true);
counterTimer.cancel();
//to set the message
TextView message =(TextView) dialog.findViewById(R.id.tvmessagedialogtext);
message.setText("Are you sure you want to Exit?");
yes = (Button) dialog.findViewById(R.id.bmessageDialogYes);
yes.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
finish();
startActivity(new Intent(QuestionActivity.this, SplashActivity.class));
}
});
no = (Button) dialog.findViewById(R.id.bmessageDialogNo);
no.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
dialog.dismiss();
nextBtn1.setEnabled(true);
nextBtn2.setEnabled(true);
nextBtn3.setEnabled(true);
nextBtn4.setEnabled(true);
}
});
dialog.show();
}