TimePickerDialog 和 AM 或 PM
我有一个 TimePickerDialog,is24Hour 设置为 false,因为我想向最终用户提供更熟悉的 12 小时格式。设置小时、分钟和 AM PM 指示器并返回时间后,如何识别最终用户选择的是 AM 还是 PM?
这是我为听众准备的:
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
mHour = hourOfDay;
mMinute = minute;
// mIsAM = WHERE CAN I GET THIS VALUE
}
};
I have a TimePickerDialog with is24Hour set to false since I want to present the end-user with the more familiar 12 hour format. When the hour, minute and AM PM indicator are set and the time is returned how can I identify whether the end-user has selected AM or PM?
This is what I have for the listener:
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
mHour = hourOfDay;
mMinute = minute;
// mIsAM = WHERE CAN I GET THIS VALUE
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
hourOfDay
将始终为 24 小时制。如果您打开对话框时将is24HourView
设置为false
,用户将不必处理 24 小时格式的时间,但 Android 会将其转换为 24 小时时间当它调用onTimeSet()
时。The
hourOfDay
will always be 24-hour. If you opened the dialog withis24HourView
set tofalse
, the user will not have to deal with 24-hour formatted times, but Android will convert that to a 24-hour time when it callsonTimeSet()
.这对我有用:
This worked for me:
我遇到了同样的问题。我的应用程序中有一个 TimePicker,在您选择时间并点击按钮后,您将进入一个显示所选时间的屏幕。选择的“时间”是正确的,但有时 AM/PM 值会与选择的相反。我最终通过更改 TimePicker 中为“hrs”参数存储的内容来修复它。
现在,当我按下按钮进入下一个屏幕时,它会正确显示所选的正确 AM/PM 值。
I was running into the same problem. I have a TimePicker in my app where after you choose a time and hit a button you'll be taken to a screen that showed what time was chosen. The "time" chosen was correct but sometimes the AM/PM value would be opposite of what was chosen. I finally fixed it by changing what I was storing for the "hrs" argument from the TimePicker.
Now when I hit the button to go to the next screen it properly showed the correct AM/PM value chosen.
简洁的 Toast 消息,其中包含用户选择的内容,显示正确的 HH:MM 格式
a neat toast message with what user selected showing proper HH:MM format
尝试这个简单的方法:
Try this simple method:
您好,您可以使用自己的格式,如下所示:
我发现“a”是格式化时间中代表 AM-PM 的代码: http://www.tutorialspoint.com/java/java_date_time.htm
Hi you could use your own format like this :
I found that "a" is the code in formating time that represents AM-PM: http://www.tutorialspoint.com/java/java_date_time.htm
如果您希望直接在时间选择器对话框中设置上午/下午,则有一个解决方法。如果您使用当前/默认时间预先填充 EditText,则此方法有效。
- 在实例化对话框之前,如果 EditText 具有 PM 时间值,请将小时部分增加 12。
sref_Time3 是 ampm 值; sref_Time1 是小时部分。
谢谢&问候。
If your expectation was to directly set AM/PM in the Time Picker Dialog box, there is a work around. This works if you pre-populate the EditText with current/default time.
- Before instantiating the dialog, increase the hour component by 12 if EditText has PM time value.
sref_Time3 is ampm value; sref_Time1 is hour component.
Thanks & Regards.
试试这个:
Try this :
这对我有用:
This works for me :
你可以试试我的解决方案,我使用了“hh:mm a”格式来设置AM/PM时间:
You can try my solution, I used "hh:mm a" format to set AM/PM time:
试试这个,希望对你有帮助
Try this, Hope it will help you
我知道现在回答已经太晚了,并且已经选择了完美的答案,但我认为对于初学者来说还不太清楚,所以发布我的答案
I know it's too late for answer, and perfect answer is already selected, But I thought it is not quite clear for beginners, so posting my answer
在使用 TimeDialog 获取时间并将其设置为 EditText 时,我发现如果“分钟”小于 0,例如:12:05,它会给出类似 12:5 的结果。在这段代码中,我已经解决了这个问题,并且如果您想要 12 小时格式,也可以正确使用 TimeDialog 来获取 AM/PM。将您的 onTimeSet() 替换为以下内容
While using the TimeDialog to get the time and set it to an EditText, I have found that if the 'minutes' are less than 0, ex: 12:05, it giving like 12:5. Here in this code I've solved that issue and also properly using TimeDialog to get AM/PM if you want 12 hour format. Replace the your onTimeSet() with the following
此方法使用现有的格式功能,并且 am/pm 和 24h 格式之间的决定取决于
timePickerDialog
打开时是否支持 24 小时。科特林示例。BR马蒂亚斯
This approach uses existing format functionality and the decision between am/pm and 24h format is based on if the
timePickerDialog
was opened with 24 hours support or not. Kotlin example.BR Matthias
无论您将 is24Hour 设置为 false 还是 true,您总是会在“onTimeSet()”方法中根据 24 小时格式收到“hourOfDay”。
例如:- 如果您将“is24Hour”设置为 false 并选择晚上 8 点,则“onTimeSet()”方法中“hourOfDay”的值将为 20。
然后你可以像这样设置你的 Am 和 Pm :
Whether you set is24Hour to false or true you always receive "hourOfDay" according to 24 hour format in "onTimeSet()" method.
Ex :- if you set "is24Hour" to false and select 8 pm then the value of "hourOfDay" in "onTimeSet()" method will be 20.
and then you can set Your Am and Pm like this :
这是获取上午、下午选定时间的最简单方法
这样您将始终获得正确的上午或下午选定时间
This is the simplest method to get selected time in AM, PM
This way you will always get the correct selected time with AM or PM
在 Kotlin 中
In Kotlin
这是一个简单的 timePickerUtil:
here is a simple timePickerUtil:
下面的 Kotlin 代码非常简单。每个人都会犯的一个常见错误是用 ** 符号表示。
The Following Code in Kotlin is very Simple and Easy. A common mistake everyone make is shown with ** Sign.
这是
TimePicker
的一个工作示例,它已从系统默认值(24 小时)转换为(AM 和 PM 的 12 小时),我已将其影响到我的FormField
:This is a working example of the
TimePicker
that has been transformed from system default (24h) to (12h with AM and PM) and I have affected that to myFormField
: