【问题标题】:problems converting CountDownTimer to my SimpleDateFormat output将 CountDownTimer 转换为我的 SimpleDateFormat 输出时出现问题
【发布时间】:2012-08-18 22:51:27
【问题描述】:

我有一个倒计时的计时器。我希望显示的格式为 00.00 或“ss.SS”。但是,我在几个小时内没有取得任何进展。如果没有 SimpleDateFormat,它会显示 01.91,然后转到 01.9。这使得它很难看,因为它会闪烁以保持视图居中。我真正想要的是一种保持格式 01.90 并且不允许删除 0 的方法。我可以在没有 SimpleDateFormat 的情况下使用我的原始代码完成此操作吗?

/*
 *  This is my original code before I tried the SimpleDateFormat
 *
 *  This code is fully functional and works good, it just keeps dropping the 0 every
 *     10 milliseconds and makes the view shake
 *
 *  getTimeSecs() could return 5, 10, 15, 30, 90 seconds converted to milliseconds
 *  getCountDownInterval() returns 10
 *  
 */

public void createTimer() {
    myCounter = new CountDownTimer(getTimeSecs(), getCountDownInterval()) {
        public void onTick(long millisUntilFinished) {
        timerIsRunning = true;

            if(millisUntilFinished < 10000) {
                TVcountDown.setText("0" + ((millisUntilFinished / 10) / 100.0));
            } else {
                TVcountDown.setText("" + ((millisUntilFinished / 10) / 100.0));
            }
        } //end onTick()

        @Override
        public void onFinish() {  
            timerIsRunning = false;
            TVcountDown.setBackgroundColor(myRes.getColor(R.color.solid_red));
            TVcountDown.setTextColor(myRes.getColor(R.color.white));
            TVcountDown.setText("Expired");

            // Make sure vibrate feature is enabled
            if(wantsVib == true) {
            vib.vibrate(300);
            }

        } //end onFinish()

    }.start();

} //end createTimer()

这是我尝试 SimpleDateFormat 后的代码

public void createTimer() {
    myCounter = new CountDownTimer(getTimeSecs(), getCountDownInterval()) {
        public void onTick(long millisUntilFinished) {
        timerIsRunning = true;
            long current = (long) ((millisUntilFinished / 10) / 100.0);
            TVcountDown.setText("" + timerDisplay.format(current));
        }

        @Override
        public void onFinish() {  
            timerIsRunning = false;
            TVcountDown.setBackgroundColor(myRes.getColor(R.color.solid_red));
            TVcountDown.setTextColor(myRes.getColor(R.color.white));
            TVcountDown.setText("Expired");

            // Make sure vibrate feature is enabled
            if(wantsVib == true) {
                vib.vibrate(300);
            }

        }

    }.start();

} //end createTimer()

我知道!我什至不认为我接近使用 SimpleDateFormat 得到它,我感到很沮丧。它运行,但只在毫秒方面倒计时。所以 15 秒显示 00.15 而不是 15.00。

我不希望有人为我编写所有代码,只需要指出正确的方向。我能找到的所有教程都涉及年、日等,我无法从中掌握概念。

我不想使用 SimpleDateFormat——因为它对我来说并不简单——只使用我的原始代码并每 10 毫秒在毫秒侧的末尾添加一个零。

提前致谢。

【问题讨论】:

    标签: java android simpledateformat countdowntimer


    【解决方案1】:

    试试这个:

    TVcountDown.setText(convertToMyFormat(millisUntilFinished));
    

    convertToMyFormat()方法:

    public String convertToMyFormat(long ms) {
        String secString, msecString;
        //constructing the sec format:
        int sec = (int) (ms / 1000);
        if(sec < 10)        secString = "0"+sec;
        else if(sec == 0)   secString = "00";
        else                secString = ""+sec;
        //constructing the msec format:
        int msec = (int) ((ms-(sec*1000))/10.0);
        if(msec < 10)       msecString = "0"+msec;
        else if(msec == 0)  msecString = "00";
        else                msecString = ""+msec;
    
        return secString+":"+msecString;
    }
    

    我不确定msec 部分是否正确,但您可以随意调整。

    【讨论】:

    • 非常感谢!像魅力一样工作!
    • @Droidxxx 很高兴我能帮上忙 :-)
    【解决方案2】:

    将数字转换为字符串,它将保持格式化。另外你可以做这样的事情

        public String NumToStr(long i){
        if (i < 10 ) {
            return ("0" + Long.toString(i));
        }
            return Long.toString(i);
        }
    

    确保“9”总是以“09”返回。现在将字符串设置为文本。

    实际上可能更容易的是这个

        if(millisUntilFinished < 10000) {
                TVcountDown.setText("0" + Long.toString((millisUntilFinished / 10) / 100.0));
        } else {
                TVcountDown.setText("" + Long.toString((millisUntilFinished / 10) / 100.0));
        }
    

    使用 Float.toString() 或 Double.toString,或任何你需要的东西。如果需要,不要害怕编写一个小函数来编辑字符串以使其显示为您想要的。

      public String KeepFirstTwoCharOfString(String string){
          //code to store first two Char into string
          // return the string containing only first 2 chars
      }
    

    【讨论】:

    • 我需要一个字符串数组还是字符数组来从millisUntilFinished中获取所有字符?或者将它们全部放在一个字符串中允许 timerDisplay.format() 正确显示它们?
    • toString 将显示任何数字作为其字符串,因此将显示双精度“1241231.05486405”。我不确切知道您的所有数字是如何工作的,但记录一些字符串以了解您是否需要编辑字符串。我再次将它扔进一个字符串中,从我想要的字符串中取出字符,然后将其扔回。小时+“:”+分钟+“:”+秒+“:”+毫秒
    • 如果您想要更多参考/解释,请转到此处stackoverflow.com/questions/11918729/…
    • 谢谢,我越来越近了。如果倒计时大于 10 秒,则显示“10.111”,除非它是偶数“10.11”,然后将最后一位数字四舍五入。如果计数器小于 10 秒,它会根据我的需要显示“09.99”。但它再次放弃了零,“09.9”
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多