简体   繁体   English

如何在日期/时间范围内显示进度/搜索栏 android

[英]How to show progress in Date/time range in progress/seek bar android

I need to show in progress bar time lapse.我需要显示进度条时间流逝。 I have start date/time and end date/time, so between start/date/time and end/date/time progress should be showed.我有开始日期/时间和结束日期/时间,所以应该显示开始/日期/时间和结束/日期/时间之间的进度。 Please help who had experience with this.请有这方面经验的人帮忙。

start========(current time)=======end 

this is how it should looks like:这就是它的样子:

You can use this simple small function:您可以使用这个简单的小 function:

fun progress(start: LocalDateTime,
             end: LocalDateTime,
             now: LocalDateTime = LocalDateTime.now()): Double = when {
    now <= start -> 0.0
    now >= end -> 1.0
    else -> start.until(now, ChronoUnit.SECONDS) /
            start.until(end, ChronoUnit.SECONDS).toDouble()
}

It has 3 different cases:它有3种不同的情况:

  1. now is before or equal to start then the progress is 0 now是在开始之前或等于start那么进度是0
  2. now is after or equal to end then the progress is 1 now在结束之后或等于end ,那么进度是1
  3. calculate (now - start) / (end - start) which gives a fraction in the range 0-1计算(now - start) / (end - start)给出0-1范围内的分数

You can then take that fraction and multiply it by 100 to get the percentage:然后,您可以将该分数乘以100以获得百分比:

val progress = progress(start, end)
val percentage = (progress * 100).roundToInt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM