修改字符串中某些子字符串的颜色

StringBuilder localStringBuilder = new StringBuilder();
localStringBuilder.append("unknow" + "\n");
localStringBuilder.append("0123456789" + "\n");
localStringBuilder.append("MAC地址未烧录" + "\n");
localStringBuilder.append("未打开wifi,获取不了MAC地址" + "\n");
localStringBuilder.append("unknow" + "\n");
localStringBuilder.append("SN未烧录" + "\n");
localStringBuilder.append("SN未烧录" + "\n");
SpannableStringBuilder style = new SpannableStringBuilder();
String text = localStringBuilder.toString();
style.append(text);
if (text.contains("MAC地址未烧录")){
int tb = text.indexOf("MAC地址未烧录");
style.setSpan(new ForegroundColorSpan(Color.RED), tb, tb+8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

for (int index = 0; index>=0 && index<text.length(); ) {
index = text.indexOf("SN未烧录", index);
if (index >= 0){
style.setSpan(new ForegroundColorSpan(Color.GREEN), index, index+5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index++;
}
}
mStringInfo.setText(style); //mStringInfo为TextView,style为按要求修改好的字符串

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页