简体   繁体   English

如何为时间选择器设置默认时间?

[英]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 ,只需setHoursetMinute的值分别更改为你想要的小时和分钟。

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.谢谢。

EDIT编辑

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.

相关问题 如何在TimePicker中设置自定义时间 - How to set custom Time in TimePicker 如何更改 Android 应用程序中时间选择器的颜色? - How do I change the color of the timepicker in my Android app? 如何以毫秒为单位获取 timepicker 选择的时间并比较 android 中两个时间选择器之间的时间差? - How do I get time picked by timepicker in milliseconds and compare time difference between the two time pickers in android? 如何在简单的记录保存应用程序中存储TimePicker数据? - How do I store TimePicker Data in my simple record keeping app? 如何将我的 textClock 设置为手机的默认时间格式? - How can I set my textClock to phone's default time format? 如何将JXTaskPane设置为默认折叠? - How do I set a JXTaskPane to be collapsed by default? 如何在 JVM 中设置默认语言环境? - How do I set the default locale in the JVM? 在 Android Studio 中执行某个操作后,如何设置我的复选框默认选中? - How do I set my checkbox checked by default after a certain action is performed in Android Studio? 如何使用 TimePicker 和 numberpicker 过滤我的回收站视图? - How can I Filter my recyclerview with TimePicker and numberpicker? 如何在 2 个不同的 EditText 中设置 2 个时间选择器? - How to set 2 timepicker in 2 different EditText?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM