更新:2018年1月31日: 有通過電話設置24小時形式在這個計算器問題,接受的答案所示setCharSequence一個更簡單的方法: Can't call setFormat24Hour of a TextClock in a Widget
老備選答案: 由於小部件視圖中編程訪問通過RemoteViews android類,並且該類不公開set24hourformat方法,則必須通過創建兩個XML佈局(每個格式要針對每種格式之一進行切換)來解決該問題,然後使用RemoteViews構造函數設置XML佈局。
有點痛苦,但只有當前的SDK API的方式。
這裏有一個例子,你會放的onUpdate():
@覆蓋 公共無效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
RemoteViews views = null;
int layoutID = 0;
if (TwentyFourHourOn){
layoutID = R.layout.widget_clock_with_24hour;
}
else {
layoutID = R.layout.widget_clock_with_12hour;
}
// the layoutID that is passed in the constructor of the
// RemoteViews object is the layout that will be loaded
// when the widget is updated.
views = new RemoteViews(context.getPackageName(), layoutID);
for (int i = 0; i < appWidgetIds.length; i++) {
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
我提供的答案完美,請將其標記爲正確答案 – 2cupsOfTech