我想在Android中反击时间

时间:2016-10-23 17:55:02

标签: java android

我创建了一个Android应用,我想制作时间计数器。存储长数据有两个长变量。

当第二次完成后,必须开始步行,然后在此过程之后进行四次必须完成的步骤。

 public void Counter(long runsecond, final long walksecond) {
    new CountDownTimer(runsecond, 1000) {

        public void onTick(long millisUntilFinished) {
            showtext.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            showtext.setText("done!");
            new CountDownTimer(walksecond, 1000) {

                public void onTick(long millisUntilFinished) {
                    mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    showtext.setText("done!");
                }
            }.start();


        }
    }.start();
}
我接受了。只计算一次。我怎么能纠正这个问题?

1 个答案:

答案 0 :(得分:0)

我觉得这样的事情可能会满足你的需求。运行和步行计数器的秒数是可配置的。重复率指定两个计数器应运行的次数。

public enum CounterType {
    RUN_COUNTER,
    WALK_COUNTER
}

private void start() {
    int runSeconds = 5;
    int walkSeconds = 5;
    int repeatRate = 4; // 2 - run, 2 walk

    startCount(runSeconds, walkSeconds, repeatRate, RUN_COUNTER);
}

private void startCount(final int runSeconds, final int walkSeconds, final int repeatRate, final CounterType actualCounter) {
    if (repeatRate == 0) {
        return;
    }

    int actualSeconds = 0;
    if (actualCounter == RUN_COUNTER) {
        actualSeconds = runSeconds;
    } else if (actualCounter == WALK_COUNTER) {
        actualSeconds = walkSeconds;
    }

    new CountDownTimer(actualSeconds * 1000, 1000) {
        @Override public void onTick(long millisUntilFinished) {
            Log.d("Counter - onTick", actualCounter.toString() + ": millis remaining: " + millisUntilFinished);
        }

        @Override public void onFinish() {
            Log.d("Counter - onFinish", actualCounter.toString());
            CounterType nextCounter = actualCounter == RUN_COUNTER ? WALK_COUNTER : RUN_COUNTER;
            startCount(runSeconds, walkSeconds, repeatRate - 1, nextCounter);
        }
    }.start();
}