如何在关闭对话框/警告框后恢复倒计时计时器

huangapple go评论70阅读模式
英文:

How to resume count down timer after closing dialogue/alert box

问题

btnHint.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 timer.cancel();// working fine
                final AlertDialog.Builder dialog = new AlertDialog.Builder(QuizActivity.this);
                dialog.setTitle("Read Carefully");
                dialog.setMessage(c.getString(7));
                dialog.setNegativeButton("Exit Hint", null);
                dialog.show();
                //how to start TIMER again after clicking 'Exit Hint';
            }
        });

I have already a countdown timer defined in onCreate as follows

timer = new CountDownTimer(90000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String time = String.valueOf(millisUntilFinished / 1000);
                tvClock.setText("Time" + "\n" + time + "/90");
                int seconds = (int) (millisUntilFinished / 1000);
                if (seconds >= 31) {
                    tvClock.setBackgroundResource(R.drawable.boxgreen);
                } else if (seconds >= 16) {
                    tvClock.setBackgroundResource(R.drawable.boxyellow);
                } else {
                    tvClock.setBackgroundResource(R.drawable.boxred);
                }
            }
英文:

I am creating a quiz app.I have a running count down timer on Quiz activity. I want to show a dialogue /alert box in between. When the dialogue box is open the count down timer is stopped. But when dialogue box is closed count down timer fails to start again. please help.

here is my code

btnHint.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 timer.cancel();// working fine
                final AlertDialog.Builder dialog = new AlertDialog.Builder(QuizActivity.this);
                dialog.setTitle("Read Carefully");
                dialog.setMessage(c.getString(7));
                dialog.setNegativeButton("Exit Hint", null);
                dialog.show();
                //how to start TIMER again after clicking 'Exit Hint'

                    }


                            });

I have already a countdown timer defined in onCreate as follows

timer = new CountDownTimer(90000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String time = String.valueOf(millisUntilFinished / 1000);
                tvClock.setText("Time" + "\n" + time + "/90");
                int seconds = (int) (millisUntilFinished / 1000);
                if (seconds >= 31) {
                    tvClock.setBackgroundResource(R.drawable.boxgreen);
                } else if (seconds >= 16) {
                    tvClock.setBackgroundResource(R.drawable.boxyellow);
                } else {
                    tvClock.setBackgroundResource(R.drawable.boxred);

                }
            }

答案1

得分: 0

首先,您需要将 CountDownTimer 的毫秒数保存到一个变量中,因为 timer.cancel(); 会重置计时器。然后,您在负按钮上创建一个 onClickListener,在其中使用保存的毫秒数重新创建计时器。

CountDownTimer timer = new CountDownTimer(10000, 10) {   // 计时器将倒计时10秒,并每10毫秒保存时间
    @Override
    public void onTick(long millisUntilFinished) {
        milis = millisUntilFinished;
        // 将时间保存到 milis 字段中
        // 在此更新您的视图显示进度
    }

    @Override
    public void onFinish() {

    }
};
timer.start();

btnHint.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        timer.cancel();  // 正常工作
        final AlertDialog.Builder dialog = new AlertDialog.Builder(QuizActivity.this);
        dialog.setTitle("仔细阅读");
        dialog.setMessage(c.getString(7));
        dialog.setNegativeButton("退出提示", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                timer = new CountDownTimer(milis, 10) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        milis = millisUntilFinished;
                        // 如果您想再次暂停计时器,请再次保存时间
                        // 在此更新您的视图显示进度
                    }

                    @Override
                    public void onFinish() {

                    }
                };
                timer.start();
            }
        });
        dialog.show();
    }
});

以上是您提供的代码部分的翻译。如果您还有其他需要翻译的内容,请继续提供。

英文:

First you have to save CountDownTimer's milis into a variable because timer.cancel(); resets the timer. Then you create onClickListener for negative button in which you recreate the timer using saved milis.

CountDownTimer timer = new CountDownTimer(10000, 10) {   //timer will count down 10 seconds and save time every 10ms
@Override
public void onTick(long millisUntilFinished) {
milis = millisUntilFinished;
//save time into field milis 
//Update your view with progress here
}
@Override
public void onFinish() {
}
};
timer.start();
btnHint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();// working fine
final AlertDialog.Builder dialog = new AlertDialog.Builder(QuizActivity.this);
dialog.setTitle("Read Carefully");
dialog.setMessage(c.getString(7));
dialog.setNegativeButton("Exit Hint", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
timer = new CountDownTimer(milis, 10) {
@Override
public void onTick(long millisUntilFinished) {
milis = millisUntilFinished;  
//save time again if you want to pause it again
//Update your view with progress here
}
@Override
public void onFinish() {
}
};
timer.start();
}
});
dialog.show();
}
});

huangapple
  • 本文由 发表于 2020年9月10日 17:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63827096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定