@Composable
fun ShowLocalTime() {
var nowTime by remember {
mutableStateOf(" ")
}
Thread {
Thread.sleep(1000)
nowTime = LocalDateTime.now().toString()
}.start()
Text(text = nowTime)
}
nowTime变量每次被线程更新的时候, Text控件属性也会随之更新.
此代码片段展示了如何在Kotlin的Composable函数中创建一个显示本地时间的函数。变量nowTime通过线程每秒更新,Text组件会随之实时刷新显示当前时间。
1271

被折叠的 条评论
为什么被折叠?



