我正在为Alarm创建一个TimePicker,我已经建立了一个时间戳,但它是24小时格式的,我想以12小时格式设置它。 我尝试以12小时格式进行,如下所示:
int hour = c.get(Calendar.HOUR);
但是,这并没有为AM PM值提供选项。我在这里查看了类似的问题。但我的方法似乎有所不同。给我最好的解决方案。这是我的Dialog代码:
public class TimeSetter extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
答案 0 :(得分:0)
这篇文章可能是您的答案。
http://www.compiletimeerror.com/2013/07/time-picker-in-android-example.html#.VVlC76Z9TFU
检查DateFormat.is24HourFormat并将boolean值保存到sharedPreference以进行用户设置。当用户呼叫选择器时,显示设置条件。