显示文本 集合
通过自定义应用显示文本的方式,增强其可用性和美观性。
@Composable fun MultipleStylesInText() { Text( buildAnnotatedString { withStyle(style = SpanStyle(color = Color.Blue)) { append("H") } append("ello ") withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Red)) { append("W") } append("orld") } ) }
设置文本部分的样式
设置文本部分的样式以提高可读性和增强用户体验。
@Composable fun AnnotatedClickableText() { val annotatedText = buildAnnotatedString { append("Go to ") // We attach this *URL* annotation to the following content // until `pop()` is called pushStringAnnotation( tag = "URL", annotation = "https://developer.android.com" ) withStyle( style = SpanStyle( color = Color.Green, fontWeight = FontWeight.Bold ) ) { append("Android Developers") } pop() append(" and check the ") pushStringAnnotation( tag = "URL", annotation = "https://developer.android.com/jetpack/compose" ) withStyle( style = SpanStyle( color = Color.Blue, fontWeight = FontWeight.Bold ) ) { append("Compose guidelines.") } pop() } ClickableText(text = annotatedText, onClick = { offset -> annotatedText.getStringAnnotations( tag = "URL", start = offset, end = offset ).firstOrNull()?.let { annotation -> // If yes, we log its value Log.d("Clicked URL", annotation.item) } }) }
在一个文本字符串中支持多个链接
在一个文本字符串中支持多个链接,为用户提供多种选择,并提高参与度。
@Composable fun LetterByLetterAnimatedText() { val text = "This text animates as though it is being typed \uD83E\uDDDE\u200D♀\uFE0F \uD83D\uDD10 \uD83D\uDC69\u200D❤\uFE0F\u200D\uD83D\uDC68 \uD83D\uDC74\uD83C\uDFFD" // Iterate over the characters. val breakIterator = remember(text) { BreakIterator.getCharacterInstance() } // Define the duration (milliseconds) of the pause before each successive // character is displayed. These pauses between characters create the // illusion of an animation. val typingDelayInMs = 50L var substringText by remember { mutableStateOf("") } LaunchedEffect(text) { // Initial start delay of the typing animation delay(1000) breakIterator.text = StringCharacterIterator(text) var nextIndex = breakIterator.next() // Iterate over the string, by index boundary while (nextIndex != BreakIterator.DONE) { substringText = text.subSequence(0, nextIndex).toString() // Go to the next logical character boundary nextIndex = breakIterator.next() delay(typingDelayInMs) } } Text(substringText) }
在用户输入时逐个字符动画显示文本
在用户输入时逐个字符动画显示文本,以提高可读性和用户参与度。
@Composable fun DialogExamples() { // ... val openAlertDialog = remember { mutableStateOf(false) } // ... when { // ... openAlertDialog.value -> { AlertDialogExample( onDismissRequest = { openAlertDialog.value = false }, onConfirmation = { openAlertDialog.value = false println("Confirmation registered") // Add logic here to handle confirmation. }, dialogTitle = "Alert dialog example", dialogText = "This is an example of an alert dialog with buttons.", icon = Icons.Default.Info ) } } } }
显示弹出消息或请求用户输入
对话框在主应用内容上方的图层上显示弹出消息或请求用户输入。
在 Compose 中绘制文本
1 分钟
了解如何使用专门用于在画布上绘制文本的 Compose API。此部分显示了在圆角矩形中绘制表情符号字体的代码。
Compose 中的可访问性
5 分钟
为您的应用添加可访问性功能,将屏幕上显示的内容转换为更适合特定需求用户的格式。了解如何通过少量工作来扩大应用的覆盖范围和多功能性。
有问题或反馈?
访问我们的常见问题解答页面,了解有关快速指南的信息,或与我们联系并告诉我们您的想法。