我根据android dev doc实现了Android时间选择器,一切正常,但我有一个奇怪的错误。我搜索了很多,没有找到任何东西,我在这里问。
以下是代码:
class TimePickerFragment 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_OF_DAY);
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
if (hourOfDay < 10) {
startTime.setText("0" + Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
} else if (minute < 10) {
startTime.setText(Integer.toString(hourOfDay) + ":0" + Integer.toString(minute));
} else if (hourOfDay < 10 && minute < 10) {
startTime.setText("0" + Integer.toString(hourOfDay) + ":0" + Integer.toString(minute));
} else if (minute == 0) {
startTime.setText(Integer.toString(hourOfDay) + ":00");
} else
startTime.setText(Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
}
}
有谁知道如何解决这个问题?
答案 0 :(得分:0)
TimePickerDialog (Context context,
int themeResId,
TimePickerDialog.OnTimeSetListener listener,
int hourOfDay,
int minute,
boolean is24HourView)
is24HourView :这是24小时视图还是上午/下午。
尝试将此参数设置为 false ,我想你会得到你想要的。
参考:https://developer.android.com/reference/android/app/TimePickerDialog.html
答案 1 :(得分:0)