ClickableText will return position of clicked character but is it possible to make each word clickable and return it?
@Composable
fun SimpleClickableText() {
ClickableText(
text = AnnotatedString("Click Me"),
onClick = { offset ->
Log.d("ClickableText", "$offset -th character is clicked.")
}
)
}
for example I have this string -> "This is a sample text" and I want to click word "sample" and return it as string
CodePudding user response:
do it like this:
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
}
}
)