我知道如何将 TimePicker 小部件设置为 24 小时,你如何为 TimePickerDialog 做同样的事情???
否则我将不得不完全创建一个新对话框?
我知道如何将 TimePicker 小部件设置为 24 小时,你如何为 TimePickerDialog 做同样的事情???
否则我将不得不完全创建一个新对话框?
基本上有一个构造函数参数
公共 TimePickerDialog(上下文上下文,TimePickerDialog.OnTimeSetListener callBack,int hourOfDay,int minute,boolean is24HourView
)
这个链接也可能对你有帮助
干杯
单击 timeButton 时显示时间选择器
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
TimePickerDialog tpd = TimePickerDialog.newInstance(
CreateBookingActivity.this,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
true
);
edit_event_time.setClickable(true);
edit_event_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
String time = sdf.format(calendar.getTime());
Log.e("time", "time " + sdf.format(calendar.getTime()));
String inputTime = time, inputHours, inputMinutes;
inputHours = inputTime.substring(0, 2);
inputMinutes = inputTime.substring(3, 5);
TimePickerDialog mTimePicker = new TimePickerDialog(DashBoardActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
if (selectedHour == 0) {
selectedHour += 12;
timeFormat = "AM";
} else if (selectedHour == 12) {
timeFormat = "PM";
} else if (selectedHour > 12) {
selectedHour -= 12;
timeFormat = "PM";
} else {
timeFormat = "AM";
}
String selectedTime = selectedHour + ":" + selectedMinute + " " + timeFormat;
edit_event_time.setText(selectedTime);
}
}, Integer.parseInt(inputHours), Integer.parseInt(inputMinutes), true);//mention true for 24 hour's time format,false for 12 hour's time format
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});