如何启动对话框新countdowntimer剩余秒? [英] how to start new countdowntimer in dialog box with remaining seconds?
问题描述
在我的应用程序,我有倒数计时器,并在同一类对话框。当有人preSS退出按钮对话框,并在其中2个按钮yes和no。我想,当有人preSS退出按钮计时暂停,如果有人preSS没有任何按键与剩余时间恢复。我知道,为了这个,我得完成这个计时器,并创建新的计时器的剩余时间。但我无法得到的剩余时间。如果有人知道如何做到这一点请大家帮帮我。
code倒数计时器 -
counterTimer =新CountDownTimer(15000,1000){
公共无效onFinish(){
如果(currentGame.getRound()== 20)
{
nextBtn1.setEnabled(假);
nextBtn2.setEnabled(假);
nextBtn3.setEnabled(假);
nextBtn4.setEnabled(假);
nextBtn5.setEnabled(假);
最终的处理程序句柄=新的处理程序();
Toast.makeText(QuestionActivity.this,时间到,Toast.LENGTH_SHORT).show();
可运行延迟=新的Runnable(){
公共无效的run(){
System.exit(0);
}
};
handle.postDelayed(延迟,3000);
}
否则,如果(currentGame.getRound()== 0)
{
currentGame.decrementScore();
最终的处理程序句柄=新的处理程序();
可运行延迟=新的Runnable(){
公共无效的run(){
processScreen();
}
};
handle.postDelayed(延迟,3000);
}
否则,如果(currentGame.getRound()< = 19)
{
nextBtn1.setEnabled(假);
nextBtn2.setEnabled(假);
nextBtn3.setEnabled(假);
nextBtn4.setEnabled(假);
nextBtn5.setEnabled(假);
currentGame.decrementScore();
最终的处理程序句柄=新的处理程序();
Toast.makeText(QuestionActivity.this,时间到,Toast.LENGTH_SHORT).show();
可运行延迟=新的Runnable(){
公共无效的run(){
processScreen();
}
};
handle.postDelayed(延迟,3000);
}
}
公共无效onTick(长millisUntilFinished){
TextView的时间=(TextView中)findViewById(R.id.timers);
time.setText(+ millisUntilFinished / 1000);
}
};
counterTimer.start();
}
code的对话框 -
如果(arg0.getId()== R.id.quit)
{
按钮是,否;
最后一个对话框对话框=新的对话框(这一点,R.style.FullHeightDialog);
dialog.setContentView(R.layout.dialog1);
dialog.setCancelable(真正的);
counterTimer.cancel();
//设置邮件
TextView的消息=(TextView中)dialog.findViewById(R.id.tvmessagedialogtext);
message.setText(你确定要退出?);
是=(按钮)dialog.findViewById(R.id.bmessageDialogYes);
yes.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v)
{
完();
startActivity(新意图(QuestionActivity.this,SplashActivity.class));
}
});
无=(按钮)dialog.findViewById(R.id.bmessageDialogNo);
no.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v)
{
dialog.dismiss();
nextBtn1.setEnabled(真正的);
nextBtn2.setEnabled(真正的);
nextBtn3.setEnabled(真正的);
nextBtn4.setEnabled(真正的);
}
});
dialog.show();
}
对话框code -
专用长remaingtime,开始时间= 15000;
MyCounter定时器;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.question);
nextBtn1 =(按钮)findViewById(R.id.answer1);
nextBtn1.setEnabled(真正的);
nextBtn1.setOnClickListener(本);
nextBtn2 =(按钮)findViewById(R.id.answer2);
nextBtn2.setEnabled(真正的);
nextBtn2.setOnClickListener(本);
nextBtn3 =(按钮)findViewById(R.id.answer3);
nextBtn3.setEnabled(真正的);
nextBtn3.setOnClickListener(本);
nextBtn4 =(按钮)findViewById(R.id.answer4);
nextBtn4.setEnabled(真正的);
nextBtn4.setOnClickListener(本);
nextBtn5 =(按钮)findViewById(R.id.quit);
nextBtn5.setEnabled(真正的);
nextBtn5.setOnClickListener(本);
定时器=新MyCounter(开始时间,1000);
timer.start();
}
如果(arg0.getId()== R.id.quit)
{
timer.cancel();
最后一个对话框对话框=新的对话框(这一点,R.style.FullHeightDialog);
dialog.setContentView(R.layout.dialog1);
dialog.setCancelable(真正的);
//设置邮件
TextView的消息=(TextView中)dialog.findViewById(R.id.tvmessagedialogtext);
message.setText(你确定要退出?);
是=(按钮)dialog.findViewById(R.id.bmessageDialogYes);
yes.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v)
{
startActivity(新意图(QuestionActivity.this,SplashActivity.class));
完();
}
});
无=(按钮)dialog.findViewById(R.id.bmessageDialogNo);
no.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v)
{
dialog.dismiss();
定时器=新MyCounter(remaingtime,1000);
timer.start();
nextBtn1.setEnabled(真正的);
nextBtn2.setEnabled(真正的);
nextBtn3.setEnabled(真正的);
nextBtn4.setEnabled(真正的);
}
});
dialog.show();
}
倒计时code -
公共类MyCounter扩展CountDownTimer
{
公共MyCounter(长millisInFuture,长countDownInterval)
{
超(millisInFuture,countDownInterval);
}
@覆盖
公共无效onTick(长millisUntilFinished)
{
remaingtime = millisUntilFinished;
时间=(TextView中)findViewById(R.id.timers);
time.setText(+ millisUntilFinished / 1000);
}
@覆盖
公共无效onFinish()
{
如果(currentGame.getRound()== 20)
{
nextBtn1.setEnabled(假);
nextBtn2.setEnabled(假);
nextBtn3.setEnabled(假);
nextBtn4.setEnabled(假);
nextBtn5.setEnabled(假);
最终的处理程序句柄=新的处理程序();
Toast.makeText(QuestionActivity.this,时间到,Toast.LENGTH_SHORT).show();
可运行延迟=新的Runnable(){
公共无效的run(){
System.exit(0);
}
};
handle.postDelayed(延迟,3000);
}
否则,如果(currentGame.getRound()== 0)
{
currentGame.decrementScore();
最终的处理程序句柄=新的处理程序();
可运行延迟=新的Runnable(){
公共无效的run(){
processScreen();
}
};
handle.postDelayed(延迟,3000);
}
否则,如果(currentGame.getRound()< = 19)
{
nextBtn1.setEnabled(假);
nextBtn2.setEnabled(假);
nextBtn3.setEnabled(假);
nextBtn4.setEnabled(假);
nextBtn5.setEnabled(假);
currentGame.decrementScore();
最终的处理程序句柄=新的处理程序();
Toast.makeText(QuestionActivity.this,时间到,Toast.LENGTH_SHORT).show();
可运行延迟=新的Runnable(){
公共无效的run()
{
processScreen();
}
};
handle.postDelayed(延迟,3000);
}
}
}
In my app i have Countdown Timer and dialog box in same class. when someone press quit button dialog box opens and in it 2 buttons yes and no. I want when someone press quit button timer pause and if someone press no button its resume with remaining seconds. I know for this i have to finish this timer and create new timer with remaining seconds. But i am unable to get remaining seconds. If someone know how to do this please help me.
Code of countdown timer-
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();
}
Code for Dialog Box-
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();
}
Dialog Box Code-
private long remaingtime, starttime = 15000;
MyCounter timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setEnabled(true);
nextBtn1.setOnClickListener(this);
nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setEnabled(true);
nextBtn2.setOnClickListener(this);
nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setEnabled(true);
nextBtn3.setOnClickListener(this);
nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setEnabled(true);
nextBtn4.setOnClickListener(this);
nextBtn5 = (Button) findViewById(R.id.quit);
nextBtn5.setEnabled(true);
nextBtn5.setOnClickListener(this);
timer = new MyCounter(starttime, 1000);
timer.start();
}
if(arg0.getId() == R.id.quit)
{
timer.cancel();
final Dialog dialog = new Dialog(this, R.style.FullHeightDialog);
dialog.setContentView(R.layout.dialog1);
dialog.setCancelable(true);
//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)
{
startActivity(new Intent(QuestionActivity.this, SplashActivity.class));
finish();
}
});
no = (Button) dialog.findViewById(R.id.bmessageDialogNo);
no.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
dialog.dismiss();
timer = new MyCounter(remaingtime, 1000);
timer.start();
nextBtn1.setEnabled(true);
nextBtn2.setEnabled(true);
nextBtn3.setEnabled(true);
nextBtn4.setEnabled(true);
}
});
dialog.show();
}
Count Down Timer Code-
public class MyCounter extends CountDownTimer
{
public MyCounter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished)
{
remaingtime = millisUntilFinished;
time = (TextView) findViewById(R.id.timers);
time.setText(""+millisUntilFinished/1000);
}
@Override
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);
}
}
}
这篇关于如何启动对话框新countdowntimer剩余秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!