在倒数计时器中添加小时数,我只有几分钟然后如何格式化它 private void updateCountDownText() { int hours = (int) (mTimeLeftInMillis / 1000) / 36000; int minutes = (int) (mTimeLeftInMillis / 1000) / 60; int seconds = (int) (mTimeLeftInMillis / 1000) % 60; String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds); mTextViewCountDown.setText(timeLeftFormatted); }
1 回答
慕斯王
TA贡献1864条经验 获得超2个赞
我建议使用TimeUnit而不是手动转换时间。
试试这个:
int hours = TimeUnit.MILLISECONDS.toHours(mTimeLeftInMillis);
int minutes = TimeUnit.MILLISECONDS.toMinutes(mTimeLeftInMillis) % TimeUnit.HOURS.toMinutes(1);
int seconds = TimeUnit.MILLISECONDS.toSeconds(mTimeLeftInMillis) % TimeUnit.MINUTES.toSeconds(1);
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
添加回答
举报
0/150
提交
取消