Android开发经验谈

Android Compose 文本多个点击事件

2024-02-26  本文已影响0人  InFatuated
val annotatedString = buildAnnotatedString {
            if (isVipList){
                append("Automatic subscription,cancel anytime\n")
                pushStringAnnotation("Terms of Use", "Terms of Use")
                append("Terms of Use")
                append(" · ")
                pushStringAnnotation("Privacy Policy", "Privacy Policy")
                append("Privacy Policy")
            }
        }


ClickableText(
    modifier = Modifier.height(52.dp).fillMaxWidth(),
    text = annotatedString,
    style = TextStyle(color = Color(0xFF999999), fontSize = 12.sp, textAlign = TextAlign.Center),
    onClick = { offset ->
        // 获取点击位置处的所有注解
        val annotations = annotatedString.getStringAnnotations(start = offset, end = offset)
        if (annotations.isNotEmpty()) {
            // 取最后一个注解,即最上层的注解
            val annotation = annotations.last()
            when (annotation.item) {
                "Terms of Use" -> {
                    // 点击了 "Terms of Use" 文本
                  
                }
                "Privacy Policy" -> {
                    // 点击了 "Privacy Policy" 文本
                   
                }
            }
        }
    }
)
上一篇 下一篇

猜你喜欢

热点阅读