TimePicker - 如何获取上午或下午?
我想根据用户在 TimePicker 中的选择设置闹钟。 TimePicker 设置为 AM/PM 模式。为了知道用户是否希望将闹钟设置为上午 10 点或晚上 10 点,我应该如何获取 AM/PM 值?
侦听器 TimePickerDialog.OnTimeSetListener
仅传递小时和分钟。
I want to set an alarm based on a user's selection in TimePicker. TimePicker is set to AM/PM mode. In order to know if a user wants his alarm to set to 10 AM or 10 PM, how should I get the AM/PM value?
The listener TimePickerDialog.OnTimeSetListener
passes only hours and minutes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
上面的答案是正确的,我找到了找到 AM PM 的最简单方法。
Above answers are right i found simplest way to find AM PM.
从
下面的函数
调用回调函数返回类似这样的内容 1:12 PM
On callback from
call below function
The functions return something like this 1:12 PM
您可以使用以下方法从 Timepicker 获取 AM/PM。
Timepicker 是一个视图组。这就是为什么我们可以获得它的子视图索引 2,这表明 AM/PM 实际上是一个按钮。这样我们就可以得到它的文本了。
You can get AM/PM from Timepicker using following method.
Timepicker is a viewgroup.That's why we can get its child view index 2 which demonstrates AM/PM is actually a button. So we can get its text.
我更愿意将此作为评论,但我没有足够的声誉。到目前为止,获取 AM 或 PM 最简洁的方法是使用三元运算符:
I would've preferred to leave this as a comment, but I don't have enough reputation. By far the most succinct way to get AM or PM is using the ternary operator:
我使用日历做到了这一点,这应该适用于所有版本:
如果您查看了两者的文档,请注意
Calendar.HOUR_OF_DAY
和Calendar.HOUR
的使用知道窍门。I did it using Calendar, and that should work on all versions:
Notice the use of
Calendar.HOUR_OF_DAY
andCalendar.HOUR
if you go to the documentation of both you'd know the trick.在这里,我从系统获取当前日期和时间,并使用日期和时间选择器对话框更新它
现在创建对话框并更新值
//创建对话框
Here I'm taking the current date and time from the system and updating it with date and time picker dialog
Now create the dialogs and update the values
//Creating dialogs
不确定这是否有帮助,但我正在做同样的事情。在这里查看我的回复
TimePickerDialog 和 AM 或 PM
Not sure if it'll help but I am kind of working on the same thing. See my reply here
TimePickerDialog and AM or PM
我认为回调将以 24 格式的小时调用,因为。 Dialog 在内部使用
TimePicker
。并且TimePicker
有 getCurrentHour 方法,返回 0-23 格式的小时。I think callback will be called with hour in 24-format because. Dialog uses
TimePicker
internally. AndTimePicker
has getCurrentHour moethod, which returns hour in 0-23 format.例如:
e.g :