要创建具有2种 colored颜色 的文本,您需要批注字符串,要使重新发送可点击,您需要重新发送和点击文本的索引
要创建计时器,可以使用LaunchedEffect,如下所示
@Composable
private fun ResendTextSample() {
val str = "Did you not receive the email? "
val length = str.length
var isTimerActive by remember {
mutableStateOf(false)
}
var time by remember { mutableStateOf("") }
LaunchedEffect(key1 = isTimerActive) {
if(isTimerActive){
var second = 0
while (second < 15) {
time = if(second <10) "0:0$second" else "0:$second"
delay(1000)
second++
}
isTimerActive = false
}
}
val annotatedLinkString = buildAnnotatedString {
append(str)
withStyle(
SpanStyle(
color = Color.Blue,
)
) {
if (!isTimerActive) {
append("Re-send")
} else {
append(time)
}
}
append("\nCheck your spam filter")
}
ClickableText(
text = annotatedLinkString,
style = TextStyle(fontSize = 20.sp),
onClick = {
if (!isTimerActive && it >= length && it <= length + 7) {
isTimerActive = true
}
}
)
}
结果.在演示中,我将最大时间设置为15秒,以显示重新发送已启用.