前言
之前写的文章《Android 14 新功能之 HighLights》里,讲到 Android 14 里推出的 HighLights 新功能能够快速完成 TextView 文字的高亮作用,并支撑动态更新。
本文将继续介绍 TextView 的另 2 处新功能:
- 运用
searchResultHighlight等针对 TextView 的查找成果进行高亮展示 - 运用
focusedSearchResultIndex针对 TextView 查找焦点高亮和移动
查找成果高亮
Android 14 推出了针对查找成果展示高亮和当时焦点高亮的新 API:
- setSearchResultHighlightColor(int color):设置一切匹配到查找关键字的文本色彩,对应的还有 android:searchResultHighlightColor attribute 能够在 xml 中装备,当然也能够运用 getSearchResultHighlightColor() 在代码中获取当时色彩值
- setSearchResultHighlights(int… ranges):设置一切匹配到查找关键字的文字规模,相同供给了 getSearchResultHighlights() 支撑在代码中获取匹配规模
下面咱们运用这些 API 进行作用实战。
首要,咱们先在代码里设置色彩,将查找到的文字为水蓝色:
class TextViewActivity : AppCompatActivity() {
companion object {
const val TEXT_LONG = "val builder = Highlights.Builder()val val val val val val val val "
}
override fun onCreate(savedInstanceState: Bundle?) {
...
with(binding.textview2) {
text = TEXT_LONG
// set highlight color for searching
searchResultHighlightColor = Color.CYAN
}
}
接着,增加 button 模仿查找:
class TextViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
// 模仿查找按钮
binding.startSearch.setOnClickListener {
binding.textview2.run {
Log.d("HighLights", "startSearch tapped" +
" and current search Color:${searchResultHighlightColor.toColorString()}"
)
// set searching ranges
setSearchResultHighlights(4, 11, 25, 32)
}
}
}
运转下代码看下作用:点击 “Search” button 之后,builder 字样呈现出水蓝色高亮。
如下的 log 也显现当时设置的高亮色为水蓝色。
2023-05-21 14:01:47.632 5513-5513/com.ellison.demo D/HighLights: startSearch tapped and current search Color:CYAN
查找焦点高亮和移动
查找焦点移动新 API 如下:
-
setFocusedSearchResultHighlightColor(int color):设置当时聚集到的匹配关键字的文本色彩,xml 中运用的 android:focusedSearchResultHighlightColor attribute 以及代码中获取该色彩值的 getFocusedSearchResultHighlightColor()
-
setFocusedSearchResultIndex(int index):设置当时聚集到的匹配关键字的索引以及 getFocusedSearchResultIndex() 获取索引的方法
注意默认的查找焦点为 FOCUSED_SEARCH_RESULT_INDEX_NONE 即 -1,意味着查找成果并未开始聚集。
然后给 TextView 增加聚集后色彩为灰色,并增加 button 模仿焦点移动:
class TextViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
with(binding.textview2) {
...
// set matched searching highlight color
focusedSearchResultHighlightColor = Color.GRAY
}
// 模仿焦点移动按钮
binding.changeSearchIndex.setOnClickListener {
binding.textview2.run {
Log.d("HighLights", "changeSearchIndex tapped" +
" and current Focused Color:${focusedSearchResultHighlightColor.toColorString()}" +
" and currentFocusedResultIndex:${focusedSearchResultIndex}"
)
// Set index to first or second
val newSearchIndex = when (focusedSearchResultIndex) {
TextView.FOCUSED_SEARCH_RESULT_INDEX_NONE, 1 -> 0
0 -> 1
else -> TextView.FOCUSED_SEARCH_RESULT_INDEX_NONE
}
Log.d("HighLights", "changeSearchIndex to :$newSearchIndex")
binding.textview2.focusedSearchResultIndex = newSearchIndex
}
}
}
看下作用:点击 “Forward” button 之后灰色高亮在两个水蓝色高亮之间切换。
log 也显现当时设置的焦点高亮为灰色,焦点 index 随着点击在第一个和第二个之间切换。
2023-05-21 14:01:51.167 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:-1
2023-05-21 14:01:51.167 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex to :0
2023-05-21 14:01:52.995 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:0
2023-05-21 14:01:52.995 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex to :1
2023-05-21 14:01:54.363 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:1
2023-05-21 14:01:54.363 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex to :0
2023-05-21 14:01:55.528 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:0
2023-05-21 14:01:55.528 5513-5513/com.ellison.demo D/HighLights: changeSearchIndex to :1
总结
上个版别 13 时 Android 针对 TextView 供给了 LineBreakConfig 换行策略的新功能,到这次 14 一次性推出了文本高亮 HighLights、查找高亮 searchResultHighlight 以及查找焦点移动 focusedSearchResultIndex 3 个新功能。
虽然已经 Android 渠道早已成熟、稳定,但 AOSP 团队仍不忘对 TextView 这个最根底的控件进行晋级和优化。能够说 TextView 既是根底,同时也是运用最频繁、最重要的,所以官方的一点点改动都显得极为可贵,开发者们需求知悉。
DEMO 源码
github.com/ellisonchan…
引荐阅读
- Android 14 新功能之 HighLights:快速完成文本高亮
- Android 13 新的换行策略和针对日文的优化
参考资料
- developer.android.com/reference/a…
- developer.android.com/reference/a…
- developer.android.com/reference/a…
- developer.android.com/reference/a…


