gpt4 book ai didi

android - Jetpack Compose Text 超链接部分文本

转载 作者:行者123 更新时间:2023-12-03 15:34:16 32 4
gpt4 key购买 nike

如何将超链接添加到 Text 组件文本的某些部分?
buildAnnotatedString我可以将链接部分设置为蓝色并加下划线,如下图所示,但我怎样才能将该部分转换为链接?
enter image description here

   val annotatedLinkString = buildAnnotatedString {
val str = "Click this link to go to web site"
val startIndex = str.indexOf("link")
val endIndex = startIndex + 4
append(str)
addStyle(
style = SpanStyle(
color = Color(0xff64B5F6),
textDecoration = TextDecoration.Underline
), start = startIndex, end = endIndex
)
}

Text(
modifier = modifier
.padding(16.dp)
.fillMaxWidth(),
text = annotatedLinkString
)
我也可以得到 Spanned但是有什么方法可以将它与 Text 一起使用?
val str: Spanned = HtmlCompat.fromHtml(
"<a href=\"http://www.github.com\">Github</a>", HtmlCompat.FROM_HTML_MODE_LEGACY
)

最佳答案

如需完整答案,您可以使用 ClickableText它返回文本的位置,以及 UriHandler在浏览器中打开 URI。

val annotatedLinkString: AnnotatedString = buildAnnotatedString {

val str = "Click this link to go to web site"
val startIndex = str.indexOf("link")
val endIndex = startIndex + 4
append(str)
addStyle(
style = SpanStyle(
color = Color(0xff64B5F6),
fontSize = 18.sp,
textDecoration = TextDecoration.Underline
), start = startIndex, end = endIndex
)

// attach a string annotation that stores a URL to the text "link"
addStringAnnotation(
tag = "URL",
annotation = "https://github.com",
start = startIndex,
end = endIndex
)

}

// UriHandler parse and opens URI inside AnnotatedString Item in Browse
val uriHandler = LocalUriHandler.current

// 🔥 Clickable text returns position of text that is clicked in onClick callback
ClickableText(
modifier = modifier
.padding(16.dp)
.fillMaxWidth(),
text = annotatedLinkString,
onClick = {
annotatedLinkString
.getStringAnnotations("URL", it, it)
.firstOrNull()?.let { stringAnnotation ->
uriHandler.openUri(stringAnnotation.item)
}
}
)

关于android - Jetpack Compose Text 超链接部分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65567412/

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