You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jetpack Compose中复用annotatedWord函数实现annotatedSentence?

问题:Jetpack Compose中复用annotatedWord逻辑实现annotatedSentence

在Jetpack Compose里实现自定义样式处理时,已编写annotatedWord函数为单个单词按条件添加SpanStyle,但实现annotatedSentence拆分句子应用样式时,希望避免重复编写判断逻辑,尝试修改annotatedWord后仍未成功复用,该如何解决?

原annotatedWord函数:

fun annotatedWord(word: String): AnnotatedString {
    return buildAnnotatedString {
        when {
            word.isDigitsOnly() -> {
                withStyle(
                    SpanStyle(
                        color = Color.Red,
                    )
                ) {
                    append(word)
                }
            }

            word.contains('@') -> {
                withStyle(
                    SpanStyle(
                        color = Color.Yellow,
                    )
                ) {
                    append(word)
                }
            }

            else -> {
                withStyle(
                    SpanStyle(
                        color = Color.Black,
                    )
                ) {
                    append(word)
                }
            }
        }
    }
}

当前重复逻辑的annotatedSentence函数:

fun annotatedSentence(sentence: String): AnnotatedString {
    return buildAnnotatedString {
        val words = sentence.split(" ")
        words.forEach { word ->
            when {
                word.isDigitsOnly() -> {
                    withStyle(
                        SpanStyle(
                            color = Color.Red,
                        )
                    ) {
                        append(word)
                    }
                }

                word.contains('@') -> {
                    withStyle(
                        SpanStyle(
                            color = Color.Yellow,
                        )
                    ) {
                        append(word)
                    }
                }

                else -> {
                    withStyle(
                        SpanStyle(
                            color = Color.Black,
                        )
                    ) {
                        append(word)
                    }
                }
            }
        }
    }
}

解决方案:直接复用annotatedWord并处理空格

无需修改原annotatedWord实现,在annotatedSentence中直接调用annotatedWord生成带样式的单词片段,再拼接即可。注意要补充单词间的空格,避免拼接后丢失间隔:

fun annotatedSentence(sentence: String): AnnotatedString {
    return buildAnnotatedString {
        val words = sentence.split(" ")
        words.forEachIndexed { index, word ->
            // 复用annotatedWord获取带样式的单词
            append(annotatedWord(word))
            // 非最后一个单词时添加空格
            if (index != words.lastIndex) {
                append(" ")
            }
        }
    }
}

进阶优化:提取样式判断逻辑(可选)

如果未来需要更灵活的样式复用,可将单词样式的判断逻辑单独提取为一个函数,让annotatedWord和annotatedSentence都基于此函数实现,进一步解耦代码:

// 单独提取样式判断逻辑
private fun getWordStyle(word: String): SpanStyle {
    return when {
        word.isDigitsOnly() -> SpanStyle(color = Color.Red)
        word.contains('@') -> SpanStyle(color = Color.Yellow)
        else -> SpanStyle(color = Color.Black)
    }
}

// 复用样式判断函数的annotatedWord
fun annotatedWord(word: String): AnnotatedString {
    return buildAnnotatedString {
        withStyle(getWordStyle(word)) {
            append(word)
        }
    }
}

// 复用样式判断函数的annotatedSentence
fun annotatedSentence(sentence: String): AnnotatedString {
    return buildAnnotatedString {
        val words = sentence.split(" ")
        words.forEachIndexed { index, word ->
            withStyle(getWordStyle(word)) {
                append(word)
            }
            if (index != words.lastIndex) {
                append(" ")
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者El Zubair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:05:06