当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现TextView字符串关键字变色的方法

Android实现TextView字符串关键字变色的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

一、字符串关键字变色

在界面显示的时候,偶尔需要将某些字符串中特定的字符串重点标出

如下图所示:

便有了下面的方法。这个方法针对于比较 固定的字符串 ,并且需要自己 计算 需要变色的文字 位置 ,代码如下:

public static charsequence setcolor(context context, string text, string text1, string text2) {
 spannablestringbuilder style = new spannablestringbuilder(text);
// 关键字“孤舟”变色,0-text1.length()
 style.setspan(new foregroundcolorspan(context.getresources().getcolor(r.color.colorprimary)), 0, text1.length(),spannable.span_exclusive_exclusive);
// 关键字“寒江雪”变色,text1.length() + 6-text1.length() + 6 + text2.length()
 style.setspan(new foregroundcolorspan(context.getresources().getcolor(r.color.coloraccent)), text1.length() + 6, text1.length() + 6 + text2.length(), spanned.span_exclusive_exclusive);
 return style;
}

二、搜索关键字变色

要使搜索关键字变色,只需要对比关键字是否和字符串之间的某些字相同,然后将相同的字改变颜色就行了。

首先说一下 如何判断一个字符串包含另一个字符串 ,有两种方法:

1. string.indexof("xxx"); ——这个方法用于查找关键字的位置,返回一个int值,没找到则返回-1;

2. string.contains("xxx"); ——这个方法是为了查看一个字符串中是否包含关键字,会返回一个boolean值。

下面这个方法用到的就是 indexof() 。


将关键字变色

代码如下:

public static charsequence matchersearchtext(int color, string string, string keyword) {
 spannablestringbuilder builder = new spannablestringbuilder(string);
 int indexof = string.indexof(keyword);
 if (indexof != -1) {
 builder.setspan(new foregroundcolorspan(color), indexof, indexof + keyword.length(), span_exclusive_exclusive);
 }
 return builder;
}

3.搜索关键字全部变色

上述方法很简单对不对?但是有一个很明显的问题,也在上图中标注出来了,就是不能使所有的关键字都变色,只能第一个变色。

下面这个方法就是要是所有的关键字都变色,就需要另外的方法了。


所有关键字变色

代码如下:

public static spannablestring matchersearchtext(int color, string text, string keyword) {
 spannablestring ss = new spannablestring(text);
​
 pattern pattern = pattern.compile(keyword);
 matcher matcher = pattern.matcher(ss);
​
 while (matcher.find()) {
 int start = matcher.start();
 int end = matcher.end();
 ss.setspan(new foregroundcolorspan(color), start, end, spanned.span_exclusive_exclusive);
 }
​
 return ss;
}

4.搜索关键字全部变色,且不区分大小写

上述方法依旧很简单对不对?那么问题又来了,上述方法虽然是把所有相同的字都标出来了,但如果是字母,肯定会遇到大小写的问题,而搜索不需要区分大小写。

首先也介绍两个string的方法: touppercase() tolowercase() ,目的是为了将字符串中的字母统一成大写或者小写。(别的字符不会发生任何改变)

要达到目的就很简单了,只需要在比较的时候,先将字母大小写统一,就能得到想要的效果。比如搜'a',所有'a'和'a'都会变色了。

注1:只是在判断的时候统一大小写,在最终显示的时候还是要显示服务器给的字符串。

注2:用这个方法就不用正则啦,简单方便。(不想用正则,在网上找了好久都没有比较明确的答案,悲剧。)


所有关键字变色,且不区分大小写

代码如下:

public static spannablestring matchersearchtitle(int color, 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), start, end,
     spanned.span_exclusive_exclusive);
 }
 return ss;
}

总结

以上就是我所总结的android实现textview字符串关键字变色的一些方法了,希望本文的内容对各位android开发者们能有所帮助,如果有疑问大家可以留言交流。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网