Example of Android implementing highlight search function

Time:2021-9-18
catalogue
  • First look at the renderings:
  • usage method:
    • 1. Common scene usage
    • 2. Use in databinding

First look at the renderings:

Whole word highlight:

Word segmentation highlight:

Here’s how to stick my seal

/**
 *Keyword highlighting
 *Textoriginal
 *Keyword requires highlighted keywords
 *Whether iscut needs word segmentation highlighting
 *Every word in the iscut = true keyword will be highlighted as long as there is one
 *Iscut = false (default) only whole words are highlighted
 **/
fun stringToHighLight(text: String, keyWord: String, isCut: Boolean = false): SpannableStringBuilder {
    val spannable = SpannableStringBuilder(text)
    try {
        var keyword: MutableList<String> = ArrayList()
        if (isCut) {
            for (i in keyWord.indices) {
                keyword.add(keyWord.substring(i, i + 1))
            }
        } else {
            keyword = arrayListOf(keyWord)
        }
        var span: CharacterStyle?
        var wordReg: String
        for (i in keyword.indices) {
            var key = ""
            if (keyword[i].contains("*") || keyword[i].contains("(") || keyword[i].contains(")")) {
                val chars = keyword[i].toCharArray()
                for (k in chars.indices) {
                    key = if (chars[k] == '*' || chars[k] == '(' || chars[k] == ')') {
                        key + "\\" + chars[k].toString()
                    } else {
                        key + chars[k].toString()
                    }
                }
                keyword[i] = key
            }
            wordReg = "(?i)" + keyword[i] 
            val pattern: Pattern = Pattern.compile(wordReg)
            val matcher: Matcher = pattern.matcher(text)
            while (matcher.find()) {
                span = ForegroundColorSpan(Color.parseColor("#4599F7"))
                spannable.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_MARK_MARK)
            }
        }
    } catch (e: Exception) {
        LogUtil.d("stringToHighLight-Error-------->$e")
    }
    return spannable
}

usage method:

1. Common scene usage

//The whole word search iscut is not required

Stringtohighlight ("original", "keyword")

//Word segmentation search

Stringtohighlight ("original", "keyword", true)

2. Use in databinding

1) : define bindingadapter


@BindingAdapter("app:setHighLightText", "app:setHightkeyWord")
fun setHighLightText(view: TextView, content: String?, keyWord: String) {
    content?.let { view.text = stringToHighLight(it, keyWord) }
}

2) : binding in XML


   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@{[email protected]/readColor:@color/noreadColor}"
        android:textSize="14sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        app:setHighLightText="@{itemmodel.title}"
        app:setHightkeyWord="@{viewmodel.keyString}" />

Done!

The above is the details of the example of Android implementing highlight search function. For more information about Android implementing highlight search, please pay attention to other relevant articles of developeppaer!