0

我不知道如何使用毫秒来制作 HH:MM:SS 倒数计时器。我需要制作一个 CountdownTimer 来告诉第二天还有多少小时、分钟和秒。这是我的代码:

  final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
   long timer = prefs.getLong("TIME", 86400000);
   new CountDownTimer(timer, 1000) {

         public void onTick(long elapsed) {

                                        time.setText(elapsed/3600000+":"+elapsed/1440000+":"+elapsed/?);
                              SharedPreferences.Editor editor = prefs.edit();
                              editor.putLong("TIME",elapsed);
                              editor.commit(); 
         }
         public void onFinish() {

         }
      }.start();

PS:另外,它必须节省到最后一次检查的时间。我急切地期待着你们的来信。先感谢您!

4

1 回答 1

5

你的计算是错误的,应该是:

final static long seconds_in_milllies = 1000L;
final static long minutes_in_millies = seconds_in_millies * 60;
final static long hours_in_millies = minutes_in_millies * 60;

// in onTick
long hours = elapsed / hours_in_millies;
elapsed %= hours_in_millies;
long minutes = elapsed / minutes_in_millies;
elapsed %= minutes_in_millies;
long seconds = elapsed / seconds_in_millies;
time.setText(hours + ":" + minutes + ":" + seconds );
于 2012-03-05T22:14:34.257 回答