google说明:
链接:https://developer.android.com/reference/android/widget/TextClock
关于某些设备会显示错误,是因为如果你设置24小时制,本机却是设置的12小时制,那么自然不能正确显示,所以只需要在java里面先判断一下是否为24小时制就行了,判断方法:textclock.is24HourModeEnabled();
可以写一个专门的方法来处理这个:
/** * 设置时钟 * @param v * @param format */ private void setFormatHour(TextClock v,String format){ if (v.is24HourModeEnabled()){ v.setFormat24Hour(format); }else { v.setFormat12Hour(format); } }
调用:
setFormatHour(tc_hhmm,"hh:mm");//当前时钟 setFormatHour(tc_yyyymmdd,"yyyy年M月dd日");//年月日 setFormatHour(tc_eeee,"EEEE");//星期
xml代码:
<TextClock android:id="@+id/tc_yyyymmdd" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="@color/white" android:format12Hour="yyyy年M月dd日" android:format24Hour="yyyy年M月dd日" android:textSize="16sp" />
<TextClock android:id="@+id/tc_eeee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:format12Hour="EEEE" android:format24Hour="EEEE" android:textSize="16sp" />
<TextClock android:id="@+id/tc_hhmm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="34dp" android:layout_alignParentBottom="true" android:format24Hour="k:mm" android:format12Hour="aah:mm" android:textColor="@color/white" android:layout_alignParentLeft="true" android:textSize="40sp" />
最后更新,后来发现其实只需要XML像上面一样设置就可以了,java代码那个反而有弊端,其中需要注意
1.EEEE是星期;
2.k:mm,是几点几分,这里写k而不是h,原因是在一些老版本的机器里面h不能正确显示时间,而查阅资料发现k能正确显示;
3.12小时制需要写上上午下午,所以aa是上午下午,完整为aah:mm;
4.yyyy年M月dd日,不需要解释吧。