“一行代码解决 Android 关键字高亮、多种颜色“ Kotlin 扩展函数版

Android   2024-03-29 09:55   183   0  

概述

showHighText 是一个 Kotlin 函数,用于在 TextView 中突出显示关键字。

工作原理:

  1. 首先,函数检查 contentText 和 keyword 是否为空。如果是,则不做任何操作并返回。

  2. 然后,函数创建一个 SpannableStringBuilder 对象。该对象允许我们将不同的样式(例如突出显示)应用于文本的部分。

  3. 接下来,函数将 keyword 参数转换为小写并存储在 lowercaseKeywords 列表中。这确保了后续的大小写不敏感匹配。(也可以自己更改一下,加一个参数动态设置)

  4. 然后,函数使用管道 ("|") 符号将 lowercaseKeywords 连接起来创建一个正则表达式。这允许我们匹配 contentText 中的任何关键字。

  5. 最后,函数使用 Matcher 对象在 contentText 中查找匹配的关键字。对于每个匹配的关键字,函数会将 ForegroundColorSpan 应用于相应的文本范围,以突出显示该关键字。

总的来说,showHighText 函数提供了一种方便的方法来突出显示 TextView 中的多个关键字,可以指定不同的颜色。

效果

image.png

image.png

代码

fun TextView.showHighText(
     contentText: String,     
     vararg keyword: String,     
     colors: List<Int> = listOf(MaterialColors.getColor(this, android.R.attr.colorPrimary)) 
) {
     if (contentText.isNullOrEmpty() || keyword.isNullOrEmpty()){ 
             this.text=""         
             return     
     }
     val stringBuilder = SpannableStringBuilder(contentText)     
     val lowercaseKeywords = keyword.map { it.lowercase(Locale.getDefault()) }
     val regex = lowercaseKeywords.joinToString("|")      
     
     val matcher = Pattern.compile(regex).matcher(contentText.lowercase(Locale.getDefault()))     
     while (matcher.find()) {
              val start = matcher.start()         
              val end = matcher.end()         
              val color = colors[start % colors.size]         
              stringBuilder.setSpan(
                  ForegroundColorSpan(color),             
                  start,             
                  end,             
                  Spanned.SPAN_INCLUSIVE_INCLUSIVE         
                        )     
     }     
     this.text = stringBuilder
}


代码解析

函数签名

fun TextView.showHighText( contentText: String, vararg keyword: String, colors: List<Int> = listOf(MaterialColors.getColor(this, android.R.attr.colorPrimary)) )


  • 该函数有三个参数:

    • contentText: 要显示在 TextView 中的主文本。

    • keyword: 要突出显示在 contentText 中的关键字,可以传入多个。

    • colors: (可选) 用于突出显示关键字的颜色列表。如果未提供,将使用主题中的默认颜色。

函数体

  1. 空值检查: 函数首先检查 contentTextkeyword 是否为空或空。如果是,则不做任何操作并返回。

  2. SpannableStringBuilder: 创建一个名为 stringBuilderSpannableStringBuilder 对象。此对象允许我们将不同的样式(例如突出显示)应用于文本的部分。

  3. 小写关键字: 将 keyword 参数转换为小写并存储在 lowercaseKeywords 列表中。这确保了后续的大小写不敏感匹配。

  4. 正则表达式: 使用管道 ("|") 符号将 lowercaseKeywords 连接起来创建一个正则表达式。这允许我们匹配 contentText 中的任何关键字。

  5. Matcher: 使用编译后的正则表达式和 contentText 的 lowercase 版本创建一个 Matcher 对象。

  6. 突出显示循环: 函数进入一个循环,直到 matcher 无法找到任何更多关键字匹配。在循环中:

  • 获取匹配关键字的 start 和 end 位置。

  • 使用 start 位置与 colors 列表大小的模来确定突出显示的颜色。这确保每个关键字都获得不同的颜色(如果提供了足够的颜色)。

  • 创建具有所选颜色的 ForegroundColorSpan 对象。

  • 将 ForegroundColorSpan 应用于匹配关键字范围(start 到 end) 在 stringBuilder 对象中。

设置文本: 最后,将 text 属性设置为 stringBuilder 对象。这将显示原始文本以及突出显示的关键字。

使用

binding.titleTextView.showHighText(title,keyword)
binding.contentTextView.showHighText(content,keyword,keyword2,keyword3,
     colors = listOf<Int>( 
        Color.parseColor("#5F8670"),
        Color.parseColor("#FF9800")
    ) 
 )


注意

假设 colors 列表至少有与关键字数量相同的元素。如果没有,则默认使用Colors中的第1个颜色


作者:TR
链接:https://juejin.cn/post/7314142205684695078
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。