فعال کردن تعاملات کاربر
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
Jetpack Compose تعامل دقیق در Text
را فعال می کند. انتخاب متن اکنون انعطافپذیرتر است و میتوان آن را در طرحبندیهای قابل ترکیب انجام داد. تعاملات کاربر در متن با دیگر طرحبندیهای قابل ترکیب متفاوت است، زیرا نمیتوانید یک اصلاحکننده به بخشی از Text
قابل نوشتن اضافه کنید. این صفحه API هایی را که تعاملات کاربر را فعال می کنند برجسته می کند.
متن را انتخاب کنید
به طور پیش فرض، composable ها قابل انتخاب نیستند، به این معنی که کاربران نمی توانند متنی را از برنامه شما انتخاب و کپی کنند. برای فعال کردن انتخاب متن، عناصر متن خود را با یک SelectionContainer
قابل ترکیب بپیچید:
@Composable
fun SelectableText() {
SelectionContainer {
Text("This text is selectable")
}
}

ممکن است بخواهید انتخاب را در قسمت های خاصی از یک منطقه قابل انتخاب غیرفعال کنید. برای انجام این کار، باید قسمت غیر قابل انتخاب را با یک DisableSelection
composable بپیچید:
@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
پیوست کنید، مانند URL متصل به یک کلمه خاص که در مرورگر باز می شود. در مواردی مانند این، باید از LinkAnnotation
استفاده کنید، که حاشیهنویسی است که بخشی از متن قابل کلیک را نشان میدهد.
با LinkAnnotation
، میتوانید یک URL را به بخشی از 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") }
}
)
}
{% کلمه به کلمه %}
{% آخر کلمه %} برای شما توصیه می شود
{% کلمه به کلمه %} {% آخر کلمه %}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-28 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-08-28 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]