사용자 상호작용 사용 설정
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
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
수정자를 추가하면 됩니다. 하지만 브라우저에서 열리는 특정 단어에 연결된 URL과 같이 Text
값의 특정 부분에 정보를 추가해야 하는 경우도 있습니다. 이러한 경우 텍스트의 클릭 가능한 부분을 나타내는 주석인 LinkAnnotation
를 사용해야 합니다.
LinkAnnotation
를 사용하면 다음 스니펫과 같이 클릭하면 자동으로 열리는 Text
컴포저블의 일부에 URL을 연결할 수 있습니다.
@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 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-08-23(UTC)
[null,null,["최종 업데이트: 2025-08-23(UTC)"],[],[],null,["# Enable user interactions\n\nJetpack 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-----------\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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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-------------------------------------------------------\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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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\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)"]]