简体   繁体   English

如果打开了其他活动,则CountDownTimer文本消失-计时器继续-Android应用

[英]CountDownTimer Text dissapears if a different Activity opened - Timer continues - android app

I have a countdowntimer that starts when a button is pressed. 我有一个倒计时计时器,当按下按钮时开始计时。

Once you press the button - the button dims, and text on the button appears and counts down to 0 (when onFinish is reached the button becomes illuminated again and the text reads 'done'). 按下按钮后-按钮将变暗,并且按钮上的文本会显示并倒数至0(达到onFinish后,按钮会再次变亮,并且文本显示为“完成”)。

I have a settings menu that allows for the addition of more timers and other settings - if a user starts a timer and it is working fine - then opens the settings menu and saves settings, they come back to the timer screen. 我有一个设置菜单,该菜单允许添加更多的计时器和其他设置-如果用户启动了一个计时器并且运行良好-然后打开设置菜单并保存设置,它们将返回到计时器屏幕。

What they see is that the button is grayed out as if the timer is still counting down (which it is), but the text is no longer there counting down and when the timer finishes the button just remains dim. 他们看到的是该按钮显示为灰色,好像计时器仍在倒计时(是),但是文本不再在那里递减计数,并且当计时器结束时,该按钮仍保持暗淡。

Is there anyway to get the text counting down to be persistent in that activity even if another Activity is opened up temporarily (like the settings menu) so it will always show the appropriate timer countdown text? 无论如何,即使暂时打开了另一个“活动”(如“设置”菜单),该文本仍可以在该活动中递减计数,从而始终显示适当的计时器倒数文本? I'm still very new to android programming so any examples are appreciated. 我还是Android编程的新手,因此不胜感激。

TimerCode looks something like this: TimerCode看起来像这样:

    //Timer Countdown
@Override
public void onTick(long millisUntilFinished) {
    button.setText((millisUntilFinished/1000)+"");
    button.getBackground().setColorFilter(android.graphics.Color.GRAY, Mode.MULTIPLY);
    button.setTextColor(Color.GREEN);
    button.setTextSize(24);
    //Timer Finishes
@Override
public void onFinish() {
    System.out.println("DONE");
    button.setTextSize(44);
    button.setText("UP");
    button.getBackground().setColorFilter(null);

Button Code looks like this: 按钮代码如下所示:

        //Right Button1
    final CountDown rButton1Timer = new CountDown(300000,200,bRightButton1);
    bRightButton1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            rButton1Timer.start();

        }
    });

Ultimately I just want the onTick and onFinish to be persistent through whatever the user does - so if he opens up the settings and changes something, when he comes back to the timers they are still counting down. 最终,我只希望onTick和onFinish在用户执行的任何操作中都保持不变-因此,如果他打开设置并进行了某些更改,当他返回计时器时,它们仍在递减计数。

Any ideas? 有任何想法吗?

onResume was the culprit. 罪魁祸首是onResume。 I have since implemented onResume with success. 此后,我已经成功实现了onResume。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM