例如,在18:00創建此timePicker(下圖)時,timePicker的timeValue將爲06:00。在19:44-> 07:44 ...因此,從AM/PM切換到24h模式後,它不會移動到當前正確的小時。我該如何改變它? 我的目標是timePicker顯示創建時的當前時間。就像我在AM/PM模式下使用它一樣。切換到24小時後,TimePicker小時不會更新
TimePicker timePicker = (TimePicker) convertView.findViewById(R.id.time_picker);
timePicker.setIs24HourView(true);
Sry爲糟糕的表述,希望你能理解我的問題。否則就問。
UPDATE:
public class ChooseTimeViewDialog extends AlertDialog {
protected Context context;
public ChooseTimeViewDialog(final Context context) {
super(context);
this.context = context;
}
public void onCreate(Bundle savedInstanceState) {
LayoutInflater inflater = this.getLayoutInflater();
View convertView = inflater.inflate(R.layout.dialog_choose_time_view, null);
setContentView(convertView);
setTitle(R.string.title_dialog_choose_time_view);
final TimePicker taskTimePicker = (TimePicker) convertView.findViewById(R.id.dctv_task_time_picker);
taskTimePicker.setIs24HourView(true);
setButton(DialogInterface.BUTTON_POSITIVE, "Choose", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int hour = taskTimePicker.getCurrentHour();
int minute = taskTimePicker.getCurrentMinute();
Calendar cal = new GregorianCalendar();
cal.set(0, 0, 0, hour, minute, 0);
((AddTaskViewActivity) context).setSelectedTime(cal.getTime());
}
});
setButton(DialogInterface.BUTTON_NEUTRAL, "No time", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AddTaskViewActivity) context).setSelectedTime(null);
}
});
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TimePicker
android:id="@+id/dctv_task_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
後,我搬到了initialiation到的onCreate和使用的setContentView(查看),在對話框的背景是透明的,它是全屏,並沒有buttons..why?
嗯,我可以修復它,但它不是一個真正的很好的解決方案,更好嗎? timePicker.setCurrentHour(new Date()。getHours()); – StupidBird
這段代碼何時執行?很多小部件在創建後都不會重新繪製。 – rds
它在一個名爲ChooseTimeDialog的自定義AlertDialog的構造函數中執行。每次打開對話框時,我都會創建一個新的對象:new ChooseTimeDialog(this).show(); – StupidBird