我知道如何将TimePicker小部件设置为24小时,你如何为TimePickerDialog做同样的事情?
否则我必须完全打开一个新的对话框吗?
答案 0 :(得分:19)
基本上有一个构造函数参数
public TimePickerDialog(Context context,TimePickerDialog.OnTimeSetListener callBack,int hourOfDay,int minute,boolean is24HourView
)
此链接也可能对您有所帮助
干杯
答案 1 :(得分:0)
点击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
);
答案 2 :(得分:0)
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();
}
});