[英]How do I set a default time to my timepicker?
I have I time picker in my code and when I press the edit text it shows up and everything looks good but I want to set a default time for my time picker so when the user opens the time picker it will be shown a specific time like "3:34 am" as a default time for the user, how can I do that?.我的代码中有时间选择器,当我按下编辑文本时,它会显示出来,一切看起来都很好,但我想为我的时间选择器设置一个默认时间,这样当用户打开时间选择器时,它将显示一个特定的时间,例如“3:34 am”作为用户的默认时间,我该怎么做? Any help will be appreciated.任何帮助将不胜感激。 { you can use java and kotlin} { 你可以使用 java 和 kotlin}
My time picker我的时间选择器
private fun showTimePicker() {
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(0)
.setTitleText("حدد الموعد الذي تريدة")
.build()
picker.show(supportFragmentManager, "AdhanNotifacations")
picker.addOnPositiveButtonClickListener {
if (picker.hour > 12) {
fajrEditTxt.setHint(String.format("%02d", picker.hour - 12) + ":"
+ String.format("%02d", picker.minute) + " PM")
} else {
fajrEditTxt.setHint(String.format("%02d", picker.hour) + ":"
+ String.format("%02d", picker.minute) + " AM")
}
calender = Calendar.getInstance()
calender[Calendar.HOUR_OF_DAY] = picker.hour
calender[Calendar.MINUTE] = picker.minute
calender[Calendar.SECOND] = 0
calender[Calendar.MILLISECOND] = 0
}
}
You were almost there, just change the value of setHour
and setMinute
to the hour and minute you want respectively.你setHour
,只需将setHour
和setMinute
的值分别更改为你想要的小时和分钟。
I made few changes to your code to show default time of 3:34am :我对您的代码进行了一些更改以显示3:34am 的默认时间:
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(3) //3 hour
.setMinute(34) //34 minutes, so 3:34am
.setTitleText("حدد الموعد الذي تريدة")
.build()
If you want to show hour in PM , just add 12 to the hour.如果您想在PM 中显示小时,只需将 12 添加到小时。 For example:例如:
setHour(15)// will show 3pm
Let me know if you have any questions.如果您有任何问题,请告诉我。 Thanks.谢谢。
If you want to show time in strict HH:MM
format you can keep your code and go as-is .如果您想以严格的HH:MM
格式显示时间,您可以保留代码并按原样进行。 But if you do not wish to be strict, then use the following code:但是,如果您不想严格,则使用以下代码:
For example, this will show 9:3PM
instead of 09:03PM
.例如,这将显示9:3PM
而不是09:03PM
。
//removed unnecesarry String.format
if(picker.hour > 12){
fajrEditTxt.setHint((picker.hour - 12).toString() + ":" + (picker.minute).toString() + " PM")
}else{
fajrEditTxt.setHint((picker.hour).toString() + ":" + (picker.minute).toString() + " AM")
}
OP asked how to access time: OP询问如何访问时间:
val time = if(picker.hour > 12){
String.format("%02d",picker.hour - 12) + ":" + String.format("%02d", picker.minute) + " PM"
}else{
String.format("%02d",picker.hour) + ":" + String.format("%02d", picker.minute) + " AM"
}
time //use time, its in HH:MM format
fajrEditTxt.setHint(time)
I'm not understanding what it is you're asking, but if you're trying to sort the times in the library, you could place the times in a list and then sort the list, or if you just want to compare the magnitude values of two different times with just a couple of lines of code, you could remove the time separators from the time strings, convert those values to numbers ( of your choice ) and compare.我不明白你在问什么,但如果你想对图书馆中的时间进行排序,你可以将时间放在一个列表中,然后对列表进行排序,或者你只是想比较大小只需几行代码即可获得两个不同时间的值,您可以从时间字符串中删除时间分隔符,将这些值转换为数字(您选择)并进行比较。 The numbers generated from the conversions have no real world values, but their magnitudes with respect to one another will always be valid.转换生成的数字没有真实世界的值,但它们相对于彼此的大小将始终有效。
Oh.哦。 Snippet editor does not support touch devices.片段编辑器不支持触摸设备。
Ok then.那好吧。
String time = "00:00";字符串时间 = "00:00";
time.replace( ":", "" ); time.replace(":", "");
Double d = Double.parseDouble( time );双 d = Double.parseDouble( 时间 );
So now you can do with d as you wish.所以现在你可以随心所欲地使用 d 了。 As you can see you don't have to bother with whether any of the values are to great.如您所见,您不必担心任何值是否很棒。 If you're dealing with a pair of valid times it's their relative magnitudes that matters.如果您正在处理一对有效时间,那么重要的是它们的相对大小。 Of course if the time is in 12 Hr format, you'll need to strip the time modifier ( A, a, P, p, Am, AM .... ) too.当然,如果时间是 12 小时格式,您也需要去掉时间修饰符(A、a、P、p、Am、AM ....)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.