For example when I create this timePicker(below) at 18:00, the timePicker's timeValue will be 06:00. At 19:44->07:44... So it doesn't move to the correct current hour after switching from AM/PM to 24h mode. How can I change that? My goal is that the timePicker shows the current time when it was created. Like it does when I use it in AM/PM mode.
TimePicker timePicker = (TimePicker) convertView.findViewById(R.id.time_picker);
timePicker.setIs24HourView(true);
Sry for the bad formulation, hope you can understand my question. Otherwise just ask.
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>
After I moved the initialiation to onCreate and used the setContentView(View), the background of the dialog is transparent, it's fullscreen and has no buttons..why?
I can use timePicker.setCurrentHour(new Date().getHours()), but that's not the solution I want. It should be possible to solve it in a better way.
Looking at the source code, this is a bug that appeared sometime after API 10:
public void setIs24HourView(Boolean is24HourView) {
if (mIs24HourView == is24HourView) {
return;
}
mIs24HourView = is24HourView;
// cache the current hour since spinner range changes
int currentHour = getCurrentHour();
updateHourControl();
// set value after spinner range is updated
setCurrentHour(currentHour);
updateAmPmControl();
}
The correct order is:
// cache the current hour since spinner range changes
int currentHour = getCurrentHour();
mIs24HourView = is24HourView;
Because getCurrentHour()
relies on mIs24HourView
to return 1-12 or 0-23...
But until the source code is updated and deployed, you don't have much of a choice. You need to call setCurrentHour()
after setIs24HourView()
yourself.
you should initialize your dialog in onCreate
In addition to the accepted answer, if you are using Xamarin then you can do the same thing by subclassing the TimePicker and overriding SetIs24HourView like so:
public class TwentyFourHourMvxTimePicker : MvxTimePicker
{
public TwentyFourHourMvxTimePicker(Context context, IAttributeSet attrs) : base (context, attrs)
{
}
public override void SetIs24HourView (Java.Lang.Boolean is24HourView)
{
var current = CurrentHour;
base.SetIs24HourView(is24HourView);
CurrentHour = current;
}
}
来源:https://stackoverflow.com/questions/13662288/timepicker-hour-doesnt-update-after-switching-to-24h