public class SetKeyWord {
/**
* 设置关键字为红色
* @param content是搜索结果
* @param keyWord是关键字
* @return
*/
public final static SpannableString setKeyWordRed(String content,String keyWord){
SpannableString s = new SpannableString(content);
Pattern p = Pattern.compile(keyWord);
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}
}
/**
* 设置关键字为红色
* @param content是搜索结果
* @param keyWord是关键字
* @return
*/
public final static SpannableString setKeyWordRed(String content,String keyWord){
SpannableString s = new SpannableString(content);
Pattern p = Pattern.compile(keyWord);
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}
}