You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让@Composable函数返回可作为委托的Timer实现?

实现Composable环境下的Timer委托

核心思路

要实现var timeLeft by Timer()的用法,需结合属性委托与Composable状态管理:

  • 用ReadWriteProperty实现委托逻辑,处理赋值(启动/重启计时器)和读取(获取剩余秒数)
  • 用remember保存计时器状态,避免重组丢失数据
  • 用LaunchedEffect触发计时协程,每秒更新剩余时间

完整代码实现

1. 定义计时器状态类

import androidx.compose.runtime.*
import kotlinx.coroutines.delay
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

// 保存计时器核心状态:剩余时间、目标时长
private class TimerState {
    var remainingSeconds by mutableStateOf(0)
    var targetSeconds = 0
}

2. 实现属性委托类

private class TimerDelegate(
    private val timerState: TimerState
) : ReadWriteProperty<ComposableContext, Int> {

    override fun getValue(thisRef: ComposableContext, property: KProperty<*>): Int {
        // 读取时直接返回剩余秒数
        return timerState.remainingSeconds
    }

    override fun setValue(thisRef: ComposableContext, property: KProperty<*>, value: Int) {
        // 赋值时更新目标时长并重置剩余时间
        timerState.targetSeconds = value
        timerState.remainingSeconds = value

        // 在Composable上下文启动计时协程,value变化时自动重启
        thisRef.LaunchedEffect(value) {
            while (timerState.remainingSeconds > 0) {
                delay(1000)
                timerState.remainingSeconds--
            }
        }
    }
}

3. 编写Timer Composable函数

@Composable
fun Timer(): ReadWriteProperty<ComposableContext, Int> {
    // 用remember保持TimerState实例,避免每次重组创建新对象
    val timerState = remember { TimerState() }
    return TimerDelegate(timerState)
}

使用示例

@Composable
fun TimerDemo() {
    var timeLeft by Timer()

    Column(
        modifier = Modifier.padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Text(text = "剩余时间:${timeLeft}秒")

        Button(onClick = {
            // 赋值5秒,启动/重启计时器
            timeLeft = 5
        }) {
            Text("启动5秒计时")
        }

        Button(onClick = {
            // 赋值10秒,重启计时器
            timeLeft = 10
        }) {
            Text("启动10秒计时")
        }
    }
}

关键细节说明

  • 赋值逻辑:每次给timeLeft赋值时,会同步更新目标时长并重置剩余时间,同时通过LaunchedEffect启动新的计时协程(key为赋值的数值,数值变化时自动重启协程)
  • 状态驱动UI:用mutableStateOf包装剩余时间,确保数值变化能触发Composable重组,实时更新UI
  • 符合需求限制:仅保留启动/重启和读取功能,无额外取消、重置逻辑

内容的提问来源于stack exchange,提问作者rslemos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:42:54