0

我需要使用复数在 Android 中显示“1 分钟前”和“2 分钟前”。问题是我有一张带后缀的时间单位地图。

 val suffixes = mapOf(
        ChronoUnit.WEEKS to context.getString(R.string.time_units_week),
        ChronoUnit.DAYS to context.getString(R.string.time_units_day),
        ChronoUnit.HOURS to context.getString(R.string.time_units_hour),
        ChronoUnit.MINUTES to context.getString(R.string.time_units_minute),
        ChronoUnit.SECONDS to context.getString(R.string.time_units_second)
)

然后我像这样使用它

fun timeLabel(now: ZonedDateTime): String = temporalUnits
        .map { Pair(it, targetTime.until(now, it)) }
        .find { it.second > 0 }
        ?.let { "${it.second} ${suffixes[it.first]}" }

如何转换它以便将时间值传递给地图?

4

1 回答 1

0

找到它,您可以简单地提供资源本身

private val suffixes = mapOf(
        ChronoUnit.WEEKS to R.plurals.time_unit_week,
        ChronoUnit.DAYS to R.plurals.time_unit_day,
        ChronoUnit.HOURS to R.plurals.time_unit_hour,
        ChronoUnit.MINUTES to R.plurals.time_unit_minute,
        ChronoUnit.SECONDS to R.plurals.time_unit_second
)

fun timeLabel(now: ZonedDateTime): String = temporalUnits
        .map { Pair(it, targetTime.until(now, it)) }
        .find { it.second > 0 }
        ?.let {
            val pluralResources = suffixes[it.first]!!
            resources.getQuantityString(pluralResources, it.second.toInt(), it.second.toInt())
        }
        ?: context.getString(R.string.time_unit_now)
于 2017-06-15T16:14:02.157 回答