//文字显示不同颜色的工具类
public class
StringColorUtil {
private Context mContext;
private SpannableStringBuilder
spBuilder;
public StringColorUtil(Context context)
{
this.mContext = context;
}
//单个关键字
//设置在wholeStr中第一次出现的keyStr的颜色
//@param wholeStr 全部文字
//@param keyStr 关键字
//@param keyStrColor 关键字的颜色
public
StringColorUtil fillColor(String wholeStr, String keyStr, int
keyStrColor) {
if
(!TextUtils.isEmpty(wholeStr) &&
!TextUtils.isEmpty(keyStr) &&
wholeStr.contains(keyStr)){
int start =
wholeStr.indexOf(keyStr);
int end = start
keyStr.length();
spBuilder = new
SpannableStringBuilder(wholeStr);
CharacterStyle charaStyle =
new
ForegroundColorSpan(mContext.getResources().getColor(keyStrColor));
spBuilder.setSpan(charaStyle,
start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
} else
{
return null;
}
}
//多个关键字
//设置在wholeStr中第一次出现的keyStr的颜色
//@param wholeStr 全部文字
//@param keyStrMap key:关键字
Value:颜色
public StringColorUtil fillColorByStr(String
wholeStr, HashMap keyStrMap) {
if(!TextUtils.isEmpty(wholeStr)){
spBuilder = new
SpannableStringBuilder(wholeStr);
Set keySet =
keyStrMap.keySet();//将Map中所有的键存入到set集合中
for(String keyStr :
keySet){
if(!TextUtils.isEmpty(keyStr)
&&
wholeStr.contains(keyStr)){
CharacterStyle charaStyle = new
ForegroundColorSpan(mContext.getResources().getColor(keyStrMap.get(keyStr)));
int start
= wholeStr.indexOf(keyStr);
int end =
start keyStr.length();
spBuilder.setSpan(charaStyle,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return this;
}else
{
return null;
}
}
//多个关键字
//设置指定位置的keyStr的颜色
//@param wholeStr 全部文字
//@param keyStringInfos 关键字的一些信息(起始位置,结束位置,颜色值)
public StringColorUtil fillColorByIndex(String wholeStr,
List keyStringInfos) {
if(!TextUtils.isEmpty(wholeStr)){
spBuilder = new
SpannableStringBuilder(wholeStr);
for(KeyStringInfo
keyStringInfo : keyStringInfos){
int start = keyStringInfo.getStart();
int end = keyStringInfo.getEnd();
if(start >=0
&& end <
wholeStr.length()
&& start
<= end){
CharacterStyle charaStyle = new
ForegroundColorSpan(mContext.getResources().getColor(keyStringInfo.getColor()));
spBuilder.setSpan(charaStyle,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return this;
}else
{
return null;
}
}
public SpannableStringBuilder getResult()
{
if
(spBuilder != null) {
return spBuilder;
}
return
null;
}
public static class KeyStringInfo{
//起始位置,结束位置,颜色值
int
start;
int
end;
int
color;
public
KeyStringInfo(int start,int end,int color){
this.start =
start;
this.end = end;
this.color =
color;
}
public int
getStart() {
return start;
}
public
void setStart(int start) {
this.start =
start;
}
public int
getEnd() {
return end;
}
public
void setEnd(int end) {
this.end = end;
}
public int
getColor() {
return color;
}
public
void setColor(int color) {
this.color =
color;
}
}
}
//调用方法
//方法1:
StringColorUtil
stringColorUtil1 = new StringColorUtil(this).fillColor("Hello
Word, 你好!","Hello",R.color.blue_color);
textView.setText(stringColorUtil1.getResult());
//方法2:
HashMap keyString = new
HashMap<>();
keyString.put("Hello",R.color.yellow_color);
keyString.put("o",R.color.red_color);
keyString.put("o",R.color.red_color);
keyString.put("你好",R.color.blue_color);
StringColorUtil
stringColorUtil = new StringColorUtil(this).fillColorByStr("Hello
Word, 你好!",keyString);
textView1.setText(stringColorUtil.getResult());
//方法3:
List keyStringInfos = new
ArrayList<>();
keyStringInfos.add(new
StringColorUtil.KeyStringInfo(4,5,R.color.yellow_color));
keyStringInfos.add(new
StringColorUtil.KeyStringInfo(7,8,R.color.red_color));
keyStringInfos.add(new
StringColorUtil.KeyStringInfo(11,13,R.color.blue_color));
StringColorUtil
stringColorUtil2 = new StringColorUtil(this).fillColorByIndex(str,keyStringInfos);
textView2.setText(stringColorUtil2.getResult());
显示效果图: