private final CharSequence getSmallTime() { Context context = getContext(); boolean is24 = DateFormat.is24HourFormat(context); LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); final char MAGIC1 = '\uEF00'; final char MAGIC2 = '\uEF01'; SimpleDateFormat sdf; String format = is24 ? d.timeFormat24 : d.timeFormat12; // Add seconds to the format string format += ":ss"; if (!format.equals(mClockFormatString)) { /* * Search for an unquoted "a" in the format string, so we can * add dummy characters around it to let us find it again after * formatting and change its size. */ if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { int a = -1; boolean quoted = false; for (int i = 0; i < format.length(); i++) { char c = format.charAt(i); if (c == ''') { quoted = !quoted; } if (!quoted && c == 'a') { a = i; break; } } if (a >= 0) { // Move a back so any whitespace before AM/PM is also in the alternate size. final int b = a; while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { a--; } format = format.substring(0, a) + MAGIC1 + format.substring(a, b) + "a" + MAGIC2 + format.substring(b + 1); } } mClockFormat = sdf = new SimpleDateFormat(format); mClockFormatString = format; } else { sdf = mClockFormat; } String result = sdf.format(mCalendar.getTime()); if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { int magic1 = result.indexOf(MAGIC1); int magic2 = result.indexOf(MAGIC2); if (magic1 >= 0 && magic2 > magic1) { SpannableStringBuilder formatted = new SpannableStringBuilder(result); if (AM_PM_STYLE == AM_PM_STYLE_GONE) { formatted.delete(magic1, magic2+1); } else { if (AM_PM_STYLE == AM_PM_STYLE_SMALL) { CharacterStyle style = new RelativeSizeSpan(0.7f); formatted.setSpan(style, magic1, magic2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); } formatted.delete(magic2, magic2 + 1); formatted.delete(magic1, magic1 + 1); } return formatted; } } return result;} protected void updateClock() { if (mDateFormat == null) { final String dateFormat = getContext().getString(R.string.system_ui_date_pattern); final Locale l = Locale.getDefault(); final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString()); mDateFormat = new SimpleDateFormat(fmt, l); } mCurrentTime.setTime(System.currentTimeMillis()); final String text = mDateFormat.format(mCurrentTime); if (!text.equals(mLastText)) { setText(text); mLastText = text; }如何实时更新秒
时间: 2024-03-13 16:43:14 浏览: 65
在代码中可以看到,时间格式化字符串中已经包含了 ":ss" 表示输出秒,因此只需要在 updateClock() 方法中,每次更新时将当前时间设置到 mCurrentTime 中即可实时更新秒数。具体实现可以参考下面的代码示例:
```java
protected void updateClock() {
if (mDateFormat == null) {
final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
final Locale l = Locale.getDefault();
final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
mDateFormat = new SimpleDateFormat(fmt, l);
}
mCurrentTime.setTime(System.currentTimeMillis());
final String text = mDateFormat.format(mCurrentTime);
if (!text.equals(mLastText)) {
setText(text);
mLastText = text;
}
// 实时更新秒
postDelayed(new Runnable() {
@Override
public void run() {
updateClock();
}
}, 1000);
}
```
这里使用了 postDelayed() 方法,每隔一秒钟就会执行一次 updateClock() 方法,从而实时更新秒数。
阅读全文