格式化倒数计时器00:00:00无法正常工作。显示00:00:00,直到停顿了一下,然后显示的剩余时间 [英] Formatting countdown timer with 00:00:00 not working. Displays 00:00:00 until paused then displays the time remaining

查看:170
本文介绍了格式化倒数计时器00:00:00无法正常工作。显示00:00:00,直到停顿了一下,然后显示的剩余时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个倒数计时器从<一个href="http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/">http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/但我不能得到它的格式工作,我想00:00:00在文章中描述。定时器将启动,并让我停下,它就会跑两分钟,但你看不到它在运行。如果暂停它,你可以看到有多少剩余时间,但你我无法得到它的工作。如果没有格式化正常工作和倒计时。有没有人有这方面的工作,如何解决它或不知道?我一直在寻找周围,找不到任何涉及一个倒数计时器这样的格式化功能。任何帮助将是AP preciated。

  TextView的timeDisplay;
  mycount的计数器;
  INT状态= 0;
  INT长度= 120000;
  长的startTime = 0;
  长currentTime的= 0;
  长timeElapsed = 0;
  长timeRemaining = 0;
  长prevTimeRemaining = 0;
  按钮控制;

    公共字符串formatTime(long millis)来{
          字符串输出=00:00:00;
          长秒=米利斯/ 1000;
          长分=秒/ 60;
          长时间=分钟/ 60;

          秒=秒%60;
          分钟=分钟%60;
          小时=小时%60;

          字符串secondsD =将String.valueOf(秒);
          字符串minutesD =将String.valueOf(分钟);
          字符串hoursD =将String.valueOf(小时);

          如果(秒-1,10)
            secondsD =0+秒;
          如果(分钟小于10)
            minutesD =0+分钟;
          如果(小时小于10)
            hoursD =0+小时;

          输出= hoursD +:+ minutesD +:+ secondsD;
          返回输出;
        }

  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.countdown);

    timeDisplay =(TextView中)findViewById(R.id.timer);
    控制=(按钮)findViewById(R.id.control);
    计数器=新mycount的(长,1000);
  }

  公共无效控制(查看视图){
    开关(州){
    情况下0:
      的startTime = System.currentTimeMillis的();
      counter.start();
      control.setText(R.string.pause);
      状态= 1;
      打破;
    情况1:
      // 暂停
      currentTime的= System.currentTimeMillis的();
      timeElapsed = currentTime的 - 的startTime;
      如果(prevTimeRemaining == 0)
        timeRemaining =长度 -  timeElapsed;
      其他
        timeRemaining = prevTimeRemaining  -  timeElapsed;
      counter.cancel();
      timeDisplay.setText(+ formatTime(timeRemaining));
      control.setText(R.string.resume);
      prevTimeRemaining = timeRemaining;

      // 恢复
      计数器=新mycount的(timeRemaining,1000);
      状态= 0;
      打破;
    案例2:
      prevTimeRemaining = 0;
      计数器=新mycount的(长,1000);
      control.setText(R.string.start);
      timeDisplay.setText(R.string.timer);
      状态= 0;
    }
  }

  公共类mycount的扩展CountDownTimer {

    公共mycount的(长millisInFuture,长countDownInterval){
      超(mil​​lisInFuture,countDownInterval);
    }

    公共无效onFinish(){
      timeDisplay.setText(完成了!);
      状态= 2;
     control.setText(R.string.restart);
    }

    公共无效onTick(长millisUntilFinished){
      timeDisplay.setText(左+ formatTime(timeRemaining));

    }

  }

}
 

解决方案

在+操作​​符有更高的precedence不是分裂。因此,尝试将一个字符串100。您可以使用括号来暗示秩序。

http://bmanolov.free.fr/javaoperators.php

I was trying to create a countdown timer from http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/ but I cannot get it to work with the format I want 00:00:00 as described in the article. The timer will start and allow me to pause it and it will run for two minutes but you cannot see it running. If you pause it you can see how much time is remaining but you I cannot get it to work. Without the formatting it works fine and counts down. Does anyone have this working or know of how to fix it? I have been searching around and cannot find anything covering a countdown timer like this with formatting. Any help would be appreciated.

  TextView timeDisplay;
  MyCount counter;
  int state = 0;
  int length = 120000;
  long startTime = 0;
  long currentTime = 0;
  long timeElapsed = 0;
  long timeRemaining = 0;
  long prevTimeRemaining = 0;
  Button control;

    public String formatTime(long millis) {
          String output = "00:00:00";
          long seconds = millis / 1000;
          long minutes = seconds / 60;
          long hours = minutes / 60;

          seconds = seconds % 60;
          minutes = minutes % 60;
          hours = hours % 60;

          String secondsD = String.valueOf(seconds);
          String minutesD = String.valueOf(minutes);
          String hoursD = String.valueOf(hours); 

          if (seconds < 10)
            secondsD = "0" + seconds;
          if (minutes < 10)
            minutesD = "0" + minutes;
          if (hours < 10)
            hoursD = "0" + hours;

          output = hoursD + " : " + minutesD + " : " + secondsD;
          return output;
        }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countdown);

    timeDisplay = (TextView) findViewById(R.id.timer);
    control = (Button) findViewById(R.id.control);
    counter = new MyCount (length, 1000);
  }

  public void control(View view) {
    switch (state) {
    case 0:
      startTime = System.currentTimeMillis();
      counter.start();
      control.setText(R.string.pause);
      state = 1;
      break;
    case 1:
      // pause
      currentTime = System.currentTimeMillis();
      timeElapsed = currentTime - startTime;
      if (prevTimeRemaining == 0)
        timeRemaining = length - timeElapsed;
      else
        timeRemaining = prevTimeRemaining - timeElapsed;
      counter.cancel();
      timeDisplay.setText("" + formatTime(timeRemaining));
      control.setText(R.string.resume);
      prevTimeRemaining = timeRemaining;

      // resume
      counter = new MyCount(timeRemaining, 1000);
      state = 0;
      break;
    case 2:
      prevTimeRemaining = 0;
      counter = new MyCount(length, 1000);
      control.setText(R.string.start);
      timeDisplay.setText(R.string.timer);
      state = 0;
    }
  }

  public class MyCount extends CountDownTimer {

    public MyCount(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }

    public void onFinish() {
      timeDisplay.setText("done!");
      state = 2;
     control.setText(R.string.restart);
    }

    public void onTick (long millisUntilFinished) {
      timeDisplay.setText ("Left: " + formatTime(timeRemaining));

    }

  }

}

解决方案

The '+' operator has higher precedence than the division. Thus you try to divide a string by 100. You can use parenthesis to imply order.

http://bmanolov.free.fr/javaoperators.php

这篇关于格式化倒数计时器00:00:00无法正常工作。显示00:00:00,直到停顿了一下,然后显示的剩余时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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