Timer display to countdown

南楼画角 提交于 2019-12-08 05:28:23

问题


right now I have my timer displaying seconds, what can I add to it to display the time in a 0:00 format?

new CountDownTimer((300 * 1000), 1000) {

                         public void onTick(long millisUntilFinished) {
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                         }

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();

回答1:


Try this:

Date date = new Date((300 * 1000)* 1000);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateFormatted = formatter.format(date);

You can also use

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");



回答2:


@Override
public void onTick(long millisUntilFinished) {
 long temp_long = millisUntilFinished / 1000;

 second = temp_long % 60;
 hour = temp_long / 3600;
 minute = (temp_long / 60) % 60;
 String tempTimer;

 tempTimer = ((hour < 10) ? "0" + hour : "" + hour)+ ((minute < 10) ? ":0" + minute : ":"+ minute)+ ((second < 10) ? ":0" + second : ":" + second);

 mTextField.setText(tempTimer);
}



回答3:


You can use android Chronometer to display countdown.

http://developer.android.com/reference/android/widget/Chronometer.html



来源:https://stackoverflow.com/questions/21447470/timer-display-to-countdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!