另外一种姿势html就不说了
实现代码
@BindingAdapter({"childcomment"})
public static void scores(TextView textView, ReplyCommentYoungModel model) {
if (model == null) {
textView.setText("");
return;
}
String childName = TextUtils.isEmpty(model.getNickname()) ? "陌生人" : model.getNickname();
;//可能被用户模拟 不存在的一个从前面找一个从后面找。来吧内容里面又?
String parentName = TextUtils.isEmpty(model.getFather_nickname()) ? "匿名的朋友" : model.getFather_nickname();
String content = "" + model.getContent();
String replyContent = "回复";
String value = String.format("%s%s%s:%s", childName, replyContent, parentName, content);
// String value = String.format("%s回复%s:%s", childName, parentName, content);
/*
String str = value.replace(childName, model.getNickname());//避免重名导致重复问题
str = str.replace(parentName, model.getFather_nickname()+"");*/
int positionChildName = value.indexOf(childName);
int positionParentName = value.indexOf(parentName, positionChildName == -1 ? 0 : positionChildName + childName.length());
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(value);
ForegroundColorSpan themeColorSpan = new ForegroundColorSpan(AppContext.getInstance().getResources().getColor(R.color.colorPinkJin));
if (positionChildName != -1) {
spannableStringBuilder.setSpan(themeColorSpan, positionChildName, positionChildName + childName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}//https://www.jianshu.com/p/f004300c6920
if (positionParentName != -1) {
spannableStringBuilder.setSpan(themeColorSpan, positionParentName, positionParentName + parentName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
//第二个只能插入 否则只会全部生效Spannable的任何修改无任何作用,这这个规则只针对 insert而且是连续的。
// spannableStringBuilder.insert(positionChildName+childName.length() + replyContent.length(), parentName);//插入又没颜色
textView.setText(spannableStringBuilder);
}
切忌别把文本框颜色也弄成粉色,我特么就犯傻了。 请问上面代码有什么问题???
正确代码如下:
@BindingAdapter({"childcomment"})
public static void scores(TextView textView, ReplyCommentYoungModel model) {
if (model == null) {
textView.setText("");
return;
}
String childName = TextUtils.isEmpty(model.getNickname()) ? "陌生人" : model.getNickname();
;//可能被用户模拟 不存在的一个从前面找一个从后面找。来吧内容里面又?
String parentName = TextUtils.isEmpty(model.getFather_nickname()) ? "匿名的朋友" : model.getFather_nickname();
String content = "" + model.getContent();
String replyContent = "回复";
String value = String.format("%s%s%s:%s", childName, replyContent, parentName, content);
// String value = String.format("%s回复%s:%s", childName, parentName, content);
/*
String str = value.replace(childName, model.getNickname());//避免重名导致重复问题
str = str.replace(parentName, model.getFather_nickname()+"");*/
int positionChildName = value.indexOf(childName);
int positionParentName = value.indexOf(parentName, positionChildName == -1 ? 0 : positionChildName + childName.length());
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(value);
if (positionChildName != -1) {
ForegroundColorSpan themeColorSpan = new ForegroundColorSpan(AppContext.getInstance().getResources().getColor(R.color.colorPinkJin));
spannableStringBuilder.setSpan(themeColorSpan, positionChildName, positionChildName + childName.length(), Spannable.SPAN_MARK_MARK);
// spannableStringBuilder.setSpan(themeColorSpan, positionChildName, positionChildName + childName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}//https://www.jianshu.com/p/f004300c6920
if (positionParentName != -1) {
ForegroundColorSpan themeColorSpan = new ForegroundColorSpan(AppContext.getInstance().getResources().getColor(R.color.colorPinkJin));
spannableStringBuilder.setSpan(themeColorSpan, positionParentName, positionParentName + parentName.length(), Spannable.SPAN_MARK_MARK);
}
//第二个只能插入 否则只会全部生效Spannable的任何修改无任何作用,这这个规则只针对 insert
// spannableStringBuilder.insert(positionChildName+childName.length() + replyContent.length(), parentName);//插入又没颜色
textView.setText(spannableStringBuilder);
}
附上一个万能的高亮颜色工具
/**
* 关键字高亮显示
*
* @param text 文字
*
* @param keyword1 文字中的关键字数组
*
* @return
*
*/
public static SpannableStringBuilder matcherSearchContent(String text, String[] keyword1) {
String[] keyword = new String[keyword1.length];
System.arraycopy(keyword1, 0, keyword, 0, keyword1.length);
SpannableStringBuilder spannable = new SpannableStringBuilder(text);
CharacterStyle span;
String wordReg;
for (int i = 0; i < keyword.length; i++) {
String key = "";
// 处理通配符问题
if (keyword[i].contains("*") || keyword[i].contains("(") || keyword[i].contains(")")) {
char[] chars = keyword[i].toCharArray();
for (int k = 0; k < chars.length; k++) {
if (chars[k] == '*' || chars[k] == '(' || chars[k] == ')') {
key = key + "\\" + String.valueOf(chars[k]);
} else {
key = key + String.valueOf(chars[k]);
}
}
keyword[i] = key;
}
wordReg = "(?i)" + keyword[i]; //忽略字母大小写
Pattern pattern = Pattern.compile(wordReg);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
span = new ForegroundColorSpan(Color.parseColor("#ff5656"));
spannable.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_MARK_MARK);
}
}
return spannable;
}
精品推荐
https://www.jianshu.com/p/1956e15c9a27
https://www.jianshu.com/p/f004300c6920