android 不区分大小写
时间: 2023-09-07 17:16:38 浏览: 148
Android TextView部分文字变色的方法可以实现不区分大小写的模糊匹配。可以使用`matcherSearchTitle`方法,该方法接收两个参数,`text`是包含需要变色文字的全部文字,`keyword`是需要变色的关键字。这个方法会将匹配到的关键字部分文字变色,并返回一个`SpannableString`对象。
以下是`matcherSearchTitle`方法的实现代码:
```java
public static SpannableString matcherSearchTitle(String text, String keyword) {
String string = text.toLowerCase();
String key = keyword.toLowerCase();
Pattern pattern = Pattern.compile(key);
Matcher matcher = pattern.matcher(string);
SpannableString ss = new SpannableString(text);
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
ss.setSpan(new ForegroundColorSpan(Color.parseColor("#77bbff")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return ss;
}
```
你可以将需要变色的文字和关键字传入该方法,然后将返回的`SpannableString`对象设置给`TextView`的文本,就能实现不区分大小写的模糊匹配文字变色效果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Android 搜索结果文字变色(不区分大小写匹配)](https://blog.csdn.net/qq_39731011/article/details/119907912)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Android Studio的代码提示设置为不区分大小写](https://blog.csdn.net/YukimineRyuu/article/details/50277597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文