TextView仅每x秒更新一次

时间:2013-09-25 09:27:51

标签: android textview countdowntimer

我有以下代码,应该每秒更改一次textview,但每2秒甚至每4秒更换一次:

final TextView tv = (TextView) findViewById(R.id.textView1);

new CountDownTimer(remain, 1000)
{
        @Override
        public void onFinish()
        {
            tv.setText("Done");
        }

        @Override
        public void onTick(long millisUntilFinished)
        {
            tv.setText(timeCalculate(millisUntilFinished / 1000));
        }
}.start();

public String timeCalculate(long ttime)
{
    long days, hours, minutes, seconds;
    String daysT = "", hoursT = "", minutesT = "", secondsT = "";

    days = (Math.round(ttime) / 86400);
    hours = (Math.round(ttime) / 3600) - (days * 24);
    minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
    seconds = Math.round(ttime) % 60;

    if (days == 1)
        daysT = String.format(Locale.getDefault(), "%d day", days);
    if (days > 1 || days == 0)
        daysT = String.format(Locale.getDefault(), "%d days", days);

    if (hours == 1)
        hoursT = String.format(Locale.getDefault(), ", %d hour", hours);
    if (hours > 1 || hours == 0)
        hoursT = String.format(Locale.getDefault(), ", %d hours", hours);

    if (minutes == 1)
        minutesT = String.format(Locale.getDefault(), ", %d minute", minutes);
    if (minutes > 1 || minutes == 0)
        minutesT = String.format(Locale.getDefault(), ", %d minutes", minutes);

    if (seconds == 1)
        secondsT = String.format(Locale.getDefault(), " %d second", seconds);
    if (seconds > 1 || seconds == 0)
        secondsT = String.format(Locale.getDefault(), " %d seconds", seconds);

    return daysT + hoursT + minutesT + secondsT + " remaining";
}

我做错了什么?

3 个答案:

答案 0 :(得分:1)

更改TextView时,请务必在TextView /包含视图上调用postInvalidate()

另外,在你发布的新方法中将字符串连接在一起时,我会在每个+之间添加空格;)

希望这会有所帮助:)

答案 1 :(得分:1)

在长

中尝试此代码mInitialTime剩余时间
mCountDownTimer = new CountDownTimer(mInitialTime, 1000) {
        StringBuilder time = new StringBuilder();

        @Override
        public void onFinish() {
            // txt_RemainingTime.setText(DateUtils.formatElapsedTime(0));
            txt_remainingTime.setText("00:00:00");


        }

        @Override
        public void onTick(long millisUntilFinished) {

            time.setLength(0);

            if (millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
                long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
                if (count > 1)
                    time.append(count).append(" days ");
                else
                    time.append(count).append(" day ");

                millisUntilFinished %= DateUtils.DAY_IN_MILLIS;

            }

            StringBuilder value = time.append(DateUtils
                    .formatElapsedTime(Math
                            .round(millisUntilFinished / 1000d)));

            String i = value.toString();
            int length = i.length();

            if (length < 6) {
                i = "00:" + i;
            }
            if (length == 7) {
                i = "0" + i;
            }
            txt_remainingTime.setText(i);
        }
    }.start();

答案 2 :(得分:0)

有趣。如果我从我的方法中获取所有代码并将其放在onTick方法中,它就可以了!相同的代码,除了删除额外的方法没有任何改变。 不知道为什么会这样。