Jetpack Compose Text 超链接文本的某些部分 [英] Jetpack Compose Text hyperlink some section of the text
问题描述
如何在 Text 组件的某些部分添加超链接?
使用 buildAnnotatedString
我可以将 link 部分设置为蓝色并加下划线,如下图所示,但我怎样才能将该部分转换为链接?
val annotatedLinkString = buildAnnotatedString {val str = "点击此链接进入网站";val startIndex = str.indexOf(链接")val 结束索引 = 开始索引 + 4追加(字符串)添加样式(样式 = 跨度样式(颜色 = 颜色(0xff64B5F6),textDecoration = TextDecoration.Underline), 开始 = 开始索引, 结束 = 结束索引)}文本(修饰符 = 修饰符.padding(16.dp).fillMaxWidth(),文本 = 带注释的LinkString)
我也可以得到 Spanned
但是有没有什么方法可以将它与 Text
一起使用?
val str: Spanned = HtmlCompat.fromHtml("<a href=\"http://www.github.com\">Github</a>", HtmlCompat.FROM_HTML_MODE_LEGACY)
如何在 Text 组件的某些部分添加超链接?
with(AnnotatedString.Builder()) {追加(链接:Jetpack Compose")//将存储 URL 的字符串注释附加到文本Jetpack Compose".addStringAnnotation(tag = "URL",annotation = "https://developer.android.com/jetpack/compose",开始 = 6,结束 = 21)}
<块引用>
tag:用来区分注解的标签
annotation:附加的字符串注释
start:范围的起始偏移量
end:
的唯一结束偏移量来源>
How can i add hyperlink to some section of the text of Text component?
With buildAnnotatedString
i can set link section blue and underlined as in image below, but how can i also turn that section into link?
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
)
I can also get Spanned
but is there any way to use it with Text
?
val str: Spanned = HtmlCompat.fromHtml(
"<a href=\"http://www.github.com\">Github</a>", HtmlCompat.FROM_HTML_MODE_LEGACY
)
How can i add hyperlink to some section of the text of Text component?
with(AnnotatedString.Builder()) {
append("link: Jetpack Compose")
// attach a string annotation that stores a URL to the text "Jetpack Compose".
addStringAnnotation(
tag = "URL",
annotation = "https://developer.android.com/jetpack/compose",
start = 6,
end = 21
)
}
tag:The tag used to distinguish annotations
annotation: The string annotation that is attached
start: The inclusive starting offset of the range
end: The exclusive end offset of the
这篇关于Jetpack Compose Text 超链接文本的某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!