当另一个CountDown计时器运行时,如何暂停一个CountDown计时器? [英] How can I pause one CountDown Timer when another one is running?

查看:162
本文介绍了当另一个CountDown计时器运行时,如何暂停一个CountDown计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有两个CountDownTimers:一个4秒,一个24秒。我希望较长的计时器每隔4秒暂停一次,较短的计时器正在运行。然后当短计时器结束时,长计时器开始倒计时。以下是两个计时器的代码:

I have two CountDownTimers in my program: a 4 second one, and 24 second one. I want the longer timer to be paused for every 4 seconds the shorter timer is running. Then when the short timer finishes, the long timer begins counting down. Here's the code for the two timers:

final CountDownTimer loop = new CountDownTimer(4000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }
                @Override
                public void onFinish() {
                    number.setVisibility(View.GONE);
                    final TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.VISIBLE);
                    prompt.setText(" Enter the number");
                    final EditText input = (EditText) findViewById(R.id.enterAnswer);
                    input.setVisibility(View.VISIBLE);
                    input.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                switch (keyCode) {
                                    case KeyEvent.KEYCODE_ENTER:
                                        Editable answer = input.getText();
                                        int finalAnswer = Integer.parseInt(String.valueOf(answer));
                                        int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
                                        input.setVisibility(View.GONE);
                                        prompt.setVisibility(View.GONE);
                                        if (finalAnswer == finalLoadG1) {
                                            score++;
                                        }

                                        number.setVisibility(View.VISIBLE);
                                        int loadG1 = generateG1.nextInt(1000000)+10000;
                                        number.setText(""+loadG1);
                                        input.getText().clear();

                                        start();

                                        return true;
                                    default:
                                }
                            }
                            return false;
                        }
                    });
                }
                }.start();


            new CountDownTimer(24000, 1000) {
                @Override
                public void onTick (long millisUntilFinished) {
                }
                @Override
                public void onFinish() {
                    TextView result = (TextView) findViewById(R.id.outcome);
                    result.setText("Score: "+ score);
                    TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.GONE);
                    final EditText input = (EditText) findViewById(R.id.enterAnswer);
                    input.setVisibility(View.GONE);
                    loop.cancel();
                    number.setVisibility(View.GONE);
                }
            }.start();

我想到命名更长的计时器,然后将name.pause()放在onTick中用于更短的计时器计时器。问题是,因为较长的计时器是在较短的计时器之后写的,所以它的名称在初始化之前是不可识别的。 Java按顺序读取内容,对吗?或者是所有语言?

I thought about naming the longer timer, then putting name.pause() in the onTick for the shorter timer. Issue is, because the longer timer is written after the shorter one, it's name is not recognizable before it has been initialized. Java reads things in order, right? Or is that all languages?

如果有人可以帮助我,我将不胜感激。

If anyone could help me out here, I'd be grateful.

对于任何能够回答的人来说,有什么方法可以防止当用户按下EditText(意味着用于int)提交任何内容时,应用程序崩溃?比如,我可以让我的应用程序在没有强制停止的情况下继续吗?

As a side question for anyone able to answer, is there any way to prevent an app from crashing when a user presses enter on an EditText (meant for an int) to submit nothing? Like, can I just have the app continue without force stopping?

非常感谢提前。

推荐答案

创建单独的方法来启动/暂停/恢复代码中的24秒计时器。您需要存储您的milliTillFinished 24秒计时器,以便您可以将其传递给恢复计时器方法。您可以在4秒计时器的onTick()方法上暂停24秒计时器并在onFinish()方法上恢复它。

Create separate methods to start/pause/resume 24 second timer in your code. You need to store Your milliTillFinished of 24 seconds timer so you can pass it to resume timer method. You can pause 24 second timer on onTick() method of 4 second timer and resume it on onFinish() method.

启动计时器:

   public void timerStart(long timeLengthMilli) {
    timer = new CountDownTimer(timeLengthMilli, 1000) {

        @Override
        public void onTick(long milliTillFinish) {
            milliLeft=milliTillFinish;
        }

暂停计时器:

public void timerPause() {
    timer.cancel();
}

恢复计时器:

private void timerResume() {
    timerStart(milliLeft);
}

这篇关于当另一个CountDown计时器运行时,如何暂停一个CountDown计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆