有没有办法以编程方式将 TimePickerDialog 中的 am 更改为 pm?
我正在寻找一种方法,在使用 12 小时格式的同时,在 Android 应用程序的 TimePickerDialog 中动态更改 AM/PM。我希望当对话框中的时间从 11 点更改为 12 点但在用户单击设置按钮之前,将 AM 切换为 PM,反之亦然。这是可能的还是我需要考虑创建自定义 TimePickerDialog 控件?我当前的代码如下。感谢您的帮助!
// Start Time
starttime = (Button)findViewById(R.id.starttime);
starttime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean is24HourFormat = use24HourFormat();
new TimePickerDialog(DutyHoursForm.this,
startTimeListener,
startDateAndTime.get(Calendar.HOUR_OF_DAY),
startDateAndTime.get(Calendar.MINUTE),
is24HourFormat).show();
}
});
TimePickerDialog.OnTimeSetListener startTimeListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startDateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
startDateAndTime.set(Calendar.MINUTE, minute);
updateStartTime();
}
};
I'm looking for a way to dynamically change the AM/PM within a TimePickerDialog for my Android application while using 12 hour format. I'm hoping to toggle the AM to PM and visa versa when the hour changes from 11 to 12 within the dialog but before the user clicks th set button. Is this possible or will I need to look into creating a custom TimePickerDialog control? My current code is below. Thanks for the help!
// Start Time
starttime = (Button)findViewById(R.id.starttime);
starttime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean is24HourFormat = use24HourFormat();
new TimePickerDialog(DutyHoursForm.this,
startTimeListener,
startDateAndTime.get(Calendar.HOUR_OF_DAY),
startDateAndTime.get(Calendar.MINUTE),
is24HourFormat).show();
}
});
TimePickerDialog.OnTimeSetListener startTimeListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startDateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
startDateAndTime.set(Calendar.MINUTE, minute);
updateStartTime();
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初始化一个变量来跟踪更改之前的小时 - 例如lastCurrentHour
那么您可以在 onTimeChanged 中执行此操作:
此处找到一个工作示例。
Initialise a variable to keep track of the hour before the change - like lastCurrentHour
then you can do this in onTimeChanged:
Find a working example here.