【问题标题】:What's the equivalent of TextClock in Jetpack Compose?Jetpack Compose 中的 TextClock 等价物是什么?
【发布时间】:2021-06-17 19:43:00
【问题描述】:

TextClock 是一个用于 Android XML 布局的小部件,它可以自行保存和显示时间。您只需添加格式和时区。

现在我在 Jetpack Compose 中看不到类似的东西。 我应该使用可组合的文本和一些时间格式化库自己实现它吗?我应该给 TextClock 充气并利用向后兼容性吗?或者有没有现成的组件?

【问题讨论】:

  • “我应该自己使用可组合的文本和一些时间格式化库来实现它吗?” - 最有可能的。 “或者有现成的组件吗?” -- 我不认为它在 Material Design 规范中,所以我不希望 Google 提供它。我不知道有第三方,但肯定有。

标签: android android-jetpack-compose


【解决方案1】:

我开始使用原始的 TextView,通过 AndroidView 可组合添加它。它确实有效,但我仍然会很感激没有“遗留”代码的答案。

@Composable
private fun TextClock(
    modifier: Modifier = Modifier,
    format12Hour: String? = null,
    format24Hour: String? = null, 
    timeZone: String? = null,
) {
    AndroidView(
        factory = { context ->
            TextClock(context).apply {
                format12Hour?.let { this.format12Hour = it }
                format24Hour?.let { this.format24Hour = it }
                timeZone?.let { this.timeZone = it }
            }
        },
        modifier = modifier
    )
}

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2014-05-08
    • 2014-06-12
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多