gpt4 book ai didi

android - 让 Jetpack Compose 中的每个单词都可以点击

转载 作者:行者123 更新时间:2023-12-05 05:46:32 26 4
gpt4 key购买 nike

ClickableText 将返回被点击字符的位置,但是否可以使每个单词可点击并返回它?

@Composable
fun SimpleClickableText() {
ClickableText(
text = AnnotatedString("Click Me"),
onClick = { offset ->
Log.d("ClickableText", "$offset -th character is clicked.")
}
)
}

例如我有这个字符串 -> "This is a sample text"我想点击单词 "sample"并将它作为字符串返回

最佳答案

这样做:

val text = AnnotatedString("Click Me")
ClickableText(
text = text,
onClick = { offset ->
val words = text.split(" ")
var cursor = 0
for (word in words) {
cursor += word.length
if(offset <= cursor) {
Log.d("ClickableText", "$word -th character is clicked.")
break
}
cursor++
}
}
)

关于android - 让 Jetpack Compose 中的每个单词都可以点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71159027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com