Hỗ trợ nhiều đường liên kết trong một chuỗi văn bản
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể hỗ trợ nhiều đường liên kết trong một chuỗi văn bản để thực hiện nhiều thao tác khi nhấp vào một tiểu mục văn bản.
Khả năng tương thích của phiên bản
Phương thức triển khai này yêu cầu bạn phải đặt minSDK của dự án thành API cấp 21 trở lên.
Phần phụ thuộc
Hiển thị nhiều đường liên kết trong một chuỗi
Đoạn mã này nhúng nhiều đường liên kết có thể nhấp vào một chuỗi văn bản duy nhất:
@ComposablefunAnnotatedStringWithLinkSample(){// Display multiple links in the textText(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(".")})}
Chỉ định kiểu văn bản và đường liên kết bằng cách truyền chúng dưới dạng đối số của hàm LinkAnnotation.Url() (bản thân hàm này được truyền dưới dạng đối số của hàm withLink()). Trình nghe lượt nhấp được tích hợp vào LinkAnnotation.Url().
Thêm văn bản bằng append() trong phần nội dung của hàm withLink.
Lặp lại quy trình này để thêm một đoạn văn bản được liên kết khác.
Kết quả
Hình 1. Ảnh chụp màn hình của một chuỗi văn bản chứa hai đường liên kết khác nhau.
Các bộ sưu tập chứa hướng dẫn này
Hướng dẫn này là một phần của các bộ sưu tập Hướng dẫn nhanh được tuyển chọn này, bao gồm các mục tiêu phát triển Android rộng hơn:
Văn bản hiển thị
Văn bản là phần chính của mọi giao diện người dùng. Tìm hiểu các cách khác nhau để bạn có thể trình bày văn bản trong ứng dụng nhằm mang lại trải nghiệm thú vị cho người dùng.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-02-22 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-02-22 UTC."],[],[],null,["\u003cbr /\u003e\n\nYou can support multiple links in a single string of text to perform different\nactions when clicking a subsection of text.\n\nVersion compatibility\n\nThis implementation requires that your project minSDK be set to API level 21 or\nhigher.\n\nDependencies\n\nDisplay multiple links in a single string\n\nThis snippet embeds multiple clickable links into a single string of text:\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\nKey points about the code\n\n- Uses the [`buildAnnotatedString`](/reference/kotlin/androidx/compose/ui/text/package-summary#buildAnnotatedString(kotlin.Function1)) function to create an annotated string of text.\n- Specifies the the link and text styling by passing them as arguments of the [`LinkAnnotation.Url()`](/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url) function (itself passed as an argument of the [`withLink()`](/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Builder#(androidx.compose.ui.text.AnnotatedString.Builder).withLink(androidx.compose.ui.text.LinkAnnotation,kotlin.Function1)) function). A click listener is built into `LinkAnnotation.Url()`.\n- Adds text using [`append()`](/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Builder#append(kotlin.CharSequence,kotlin.Int,kotlin.Int)) in the body of the `withLink` function.\n- Repeats this process to add another linked text segment.\n\nResults **Figure 1.** A screenshot of one text string containing two different links.\n\nCollections that contain this guide\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\nDisplay text \nText is a central piece of any UI. Find out different ways you can present text in your app to provide a delightful user experience. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-text) \n\nHave questions or feedback \nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]