0

我有以下代码应该每秒更改一次文本视图,但仅每 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";
}

我究竟做错了什么?

4

3 回答 3

1

当您更改 TextView 时,请确保调用postInvalidate()TextView / 包含视图。

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

希望这可以帮助 :)

于 2013-09-25T09:32:52.347 回答
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();
于 2013-09-25T09:42:26.640 回答
0

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

于 2013-09-25T12:32:35.790 回答