Android 14 新功能之 TextView 搜索结果高亮和焦点移动

前言

之前写的文章《Android 14 新功能之 HighLights》,讲到 Android 14 里推出的 HighLights 新功能可以快速实现 TextView 文字的高亮效果,并支持动态更新

本文将继续介绍 TextView 的另 2 处新功能:

  1. 使用 searchResultHighlight 等针对 TextView 的搜索结果进行高亮展示

  2. 使用 focusedSearchResultIndex 针对 TextView 搜索焦点高亮和移动

1

搜索结果高亮

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 : startSearch tapped and current search Color:CYAN

2

搜索焦点高亮和移动

搜索焦点移动新 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 : changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:-1
2023-05-21 : changeSearchIndex to :0
2023-05-21 : changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:0
2023-05-21 : changeSearchIndex to :1
2023-05-21 : changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:1
2023-05-21 : changeSearchIndex to :0
2023-05-21 : changeSearchIndex tapped and current Focused Color:GREY and currentFocusedResultIndex:0
2023-05-21 : changeSearchIndex to :1

3

总结

上个版本 13 时 Android 针对 TextView 提供了 LineBreakConfig 换行策略的新功能,到这次 14 一次性推出了文本高亮 HighLights、搜索高亮 searchResultHighlight 以及搜索焦点移动 focusedSearchResultIndex 3 个新功能。

虽然已经 Android 平台早已成熟、稳定,但 AOSP 团队仍不忘对 TextView 这个最基础的控件进行升级和优化。可以说 TextView 既是基础,同时也是使用最频繁、最重要的,所以官方的一点点改动都显得极为难得,开发者们需要知悉。

DEMO 源码:

https://github.com/ellisonchan/AndroidUDemo

参考资料

https://developer.android.com/reference/android/widget/TextView#setSearchResultHighlightColor(int)

转自:Android 14 新功能之 TextView 搜索结果高亮和焦点移动~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用SearchView和RecyclerView来实现Android中的搜索功能,并且在搜索结果中高亮显示匹配的文本。下面是实现的简要步骤: 1. 首先,在你的布局文件中添加SearchView和RecyclerView。 2. 在Activity或Fragment中,找到SearchView并设置搜索监听器。在监听器的onQueryTextChange方法中,获取用户输入的搜索关键字,并更RecyclerView的数据源。 3. 在RecyclerView的Adapter中,实现一个过滤方法来根据搜索关键字过滤数据源。这个过滤方法可以使用String类的contains方法来判断是否存在匹配的文本。 4. 在过滤方法中,你可以使用SpannableString类来对匹配的文本进行高亮处理。使用ForegroundColorSpan类来设置高亮文本的颜色。 这是一个简单的示例代码,帮助你更好地理解: ```java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> dataList; private List<String> filteredDataList; private String searchText; // Adapter构造函数和其他方法... public void filter(String text) { searchText = text.toLowerCase(); filteredDataList.clear(); if (searchText.isEmpty()) { filteredDataList.addAll(dataList); } else { for (String item : dataList) { if (item.toLowerCase().contains(searchText)) { filteredDataList.add(item); } } } notifyDataSetChanged(); } // ViewHolder和其他方法的实现... @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { String item = filteredDataList.get(position); // 高亮处理 if (!searchText.isEmpty()) { int startPos = item.toLowerCase().indexOf(searchText); int endPos = startPos + searchText.length(); if (startPos != -1) { SpannableString spannableString = new SpannableString(item); ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED); spannableString.setSpan(colorSpan, startPos, endPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holder.textView.setText(spannableString); } else { holder.textView.setText(item); } } else { holder.textView.setText(item); } } } ``` 这只是一个简单的示例,你可以根据实际需求进行修改和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值