实现用户互动
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Jetpack Compose 支持 Text
中的精细互动。文字选择现在更加灵活,并且可以跨各种可组合项布局进行选择。文字中的用户互动与其他可组合项布局不同,因为您无法为 Text
可组合项的某一部分添加修饰符。本页将重点介绍支持用户互动的 API。
选择文字
默认情况下,可组合项不可选择,这意味着用户无法从您的应用中选择和复制文字。如需启用文字选择,请使用 SelectionContainer
可组合项封装文字元素:
@Composable
fun SelectableText() {
SelectionContainer {
Text("This text is selectable")
}
}
您可能想为可选择区域的特定部分停用选择功能。如果要执行此操作,您需要使用 DisableSelection
可组合项来封装不可选择的部分:
@Composable
fun PartiallySelectableText() {
SelectionContainer {
Column {
Text("This text is selectable")
Text("This one too")
Text("This one as well")
DisableSelection {
Text("But not this one")
Text("Neither this one")
}
Text("But again, you can select this one")
Text("And this one too")
}
}
}
使用 LinkAnnotation
创建可点击的文本部分
如需监听 Text
的点击次数,您可以添加 clickable
修饰符。不过,您可能想向 Text
值的某一部分附加额外信息,例如向特定字词附加可在浏览器中打开的网址。在这种情况下,您需要使用 LinkAnnotation
,这是一种表示文本中可点击部分的注释。
借助 LinkAnnotation
,您可以将网址附加到 Text
可组合项的一部分,以便在点击后自动打开,如以下代码段所示:
@Composable
fun AnnotatedStringWithLinkSample() {
// Display multiple links in the text
Text(
buildAnnotatedString {
append("Go to the ")
withLink(
LinkAnnotation.Url(
"https://developer.android.com/",
TextLinkStyles(style = SpanStyle(color = Color.Blue))
)
) {
append("Android Developers ")
}
append("website, and check out the")
withLink(
LinkAnnotation.Url(
"https://developer.android.com/jetpack/compose",
TextLinkStyles(style = SpanStyle(color = Color.Green))
)
) {
append("Compose guidance")
}
append(".")
}
)
}
您还可以配置自定义操作,以响应用户对 Text
可组合项的某一部分的点击。在以下代码段中,当用户点击“Jetpack Compose”时,系统会显示一个链接,如果用户点击该链接,系统会记录相关指标:
@Composable
fun AnnotatedStringWithListenerSample() {
// Display a link in the text and log metrics whenever user clicks on it. In that case we handle
// the link using openUri method of the LocalUriHandler
val uriHandler = LocalUriHandler.current
Text(
buildAnnotatedString {
append("Build better apps faster with ")
val link =
LinkAnnotation.Url(
"https://developer.android.com/jetpack/compose",
TextLinkStyles(SpanStyle(color = Color.Blue))
) {
val url = (it as LinkAnnotation.Url).url
// log some metrics
uriHandler.openUri(url)
}
withLink(link) { append("Jetpack Compose") }
}
)
}
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[],[],null,["Jetpack Compose enables fine-grained interactivity in `Text`. Text selection is\nnow more flexible and can be done across composable layouts. User interactions\nin text are different from other composable layouts, as you can't add a modifier\nto a portion of a `Text` composable. This page highlights the APIs\nthat enable user interactions.\n\nSelect text\n\nBy default, composables aren't selectable, which means that users can't\nselect and copy text from your app. To enable text selection, wrap\nyour text elements with a [`SelectionContainer`](/reference/kotlin/androidx/compose/foundation/text/selection/package-summary#SelectionContainer(androidx.compose.ui.Modifier,kotlin.Function0)) composable:\n\n\n```kotlin\n@Composable\nfun SelectableText() {\n SelectionContainer {\n Text(\"This text is selectable\")\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt#L407-L412\n```\n\n\u003cbr /\u003e\n\nYou may want to disable selection on specific parts of a selectable area. To do\nso, you need to wrap the unselectable part with a [`DisableSelection`](/reference/kotlin/androidx/compose/foundation/text/selection/package-summary#DisableSelection(kotlin.Function0))\ncomposable:\n\n\n```kotlin\n@Composable\nfun PartiallySelectableText() {\n SelectionContainer {\n Column {\n Text(\"This text is selectable\")\n Text(\"This one too\")\n Text(\"This one as well\")\n DisableSelection {\n Text(\"But not this one\")\n Text(\"Neither this one\")\n }\n Text(\"But again, you can select this one\")\n Text(\"And this one too\")\n }\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt#L418-L433\n```\n\n\u003cbr /\u003e\n\nCreate clickable sections of text with `LinkAnnotation`\n\nTo listen for clicks on `Text`, you can add the [`clickable`](/reference/kotlin/androidx/compose/foundation/package-summary#(androidx.compose.ui.Modifier).clickable(kotlin.Boolean,kotlin.String,androidx.compose.ui.semantics.Role,kotlin.Function0))\nmodifier. However, you may want to attach extra information to a certain part of\nthe `Text` value, like a URL attached to a certain word to be opened in a\nbrowser. In cases like this, you need to use a [`LinkAnnotation`](/reference/kotlin/androidx/compose/ui/text/LinkAnnotation), which is\nan annotation that represents a clickable part of the text.\n\nWith `LinkAnnotation`, you can attach a URL to a part of a `Text` composable\nthat automatically opens once clicked, as shown in the following snippet:\n\n\n```kotlin\n@Composable\nfun AnnotatedStringWithLinkSample() {\n // Display multiple links in the text\n Text(\n buildAnnotatedString {\n append(\"Go to the \")\n withLink(\n LinkAnnotation.Url(\n \"https://developer.android.com/\",\n TextLinkStyles(style = SpanStyle(color = Color.Blue))\n )\n ) {\n append(\"Android Developers \")\n }\n append(\"website, and check out the\")\n withLink(\n LinkAnnotation.Url(\n \"https://developer.android.com/jetpack/compose\",\n TextLinkStyles(style = SpanStyle(color = Color.Green))\n )\n ) {\n append(\"Compose guidance\")\n }\n append(\".\")\n }\n )\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt#L564-L590\n```\n\n\u003cbr /\u003e\n\nYou can also configure a custom action in response to a user click on a part of\nthe `Text` composable. In the following snippet, when the user clicks on\n\"Jetpack Compose,\" a link is displayed, and metrics are logged if the user\nclicks the link:\n\n\n```kotlin\n@Composable\nfun AnnotatedStringWithListenerSample() {\n // Display a link in the text and log metrics whenever user clicks on it. In that case we handle\n // the link using openUri method of the LocalUriHandler\n val uriHandler = LocalUriHandler.current\n Text(\n buildAnnotatedString {\n append(\"Build better apps faster with \")\n val link =\n LinkAnnotation.Url(\n \"https://developer.android.com/jetpack/compose\",\n TextLinkStyles(SpanStyle(color = Color.Blue))\n ) {\n val url = (it as LinkAnnotation.Url).url\n // log some metrics\n uriHandler.openUri(url)\n }\n withLink(link) { append(\"Jetpack Compose\") }\n }\n )\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt#L594-L614\n```\n\n\u003cbr /\u003e\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Semantics in Compose](/develop/ui/compose/semantics)\n- [Accessibility in Compose](/develop/ui/compose/accessibility)\n- [Material Design 2 in Compose](/develop/ui/compose/designsystems/material)"]]