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

このコードを試してください mInitialTime long の残り時間

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 に答える