效果:
/** 将类似"¥ 1.5 元"的文本中的数字标红 或标蓝 或标绿*/
public CharSequence getContent(){
String input=text;
if(input==null || input.isEmpty()) content="";
else if(content==null){
//"¥ *-?\\d+(\\.\\d+)? *元" 你想要标红的文本部分
if(pattern==null) pattern=Pattern.compile("¥ *-?\\d+(\\.\\d+)? *元");
Matcher m=pattern.matcher(input);
SpannableStringBuilder builder=null;
int start=0;
int end=0;
while(m.find()){
if(builder==null) builder=new SpannableStringBuilder();
end=m.start();
if(end>start){
builder.append(input.substring(start, end));
start=builder.length();
}
String g=m.group();
builder.append(g.substring(1, g.length()-1).trim()).append("元");
/**
红色 #e10130 修改这个颜色值选择你想要的颜色
蓝色 #0083E5 我的项目设置的是蓝色
*/
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#e10130")),
start, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
start=m.end();
}
if(builder==null) content=input;
else{
if(start<input.length()) builder.append(input.substring(start));
content=builder;
}
}
return content;
}
分割线======================================================================================
完整代码:
model类 HotNews:
public class HotNews
{
@SerializedName("text")
public String text;
@SerializedName("add_time")
public String addTime;
@SerializedName("id")
public int id;
@SerializedName("link")
public String link;
private CharSequence content;
static Pattern pattern;
/** 将类似"¥ 1.5 元"的文本中的数字标红 或标蓝 或标绿*/
public CharSequence getContent(){
String input=text;
if(input==null || input.isEmpty()) content="";
else if(content==null){
if(pattern==null) pattern=Pattern.compile("¥ *-?\\d+(\\.\\d+)? *元");
Matcher m=pattern.matcher(input);
SpannableStringBuilder builder=null;
int start=0;
int end=0;
while(m.find()){
if(builder==null) builder=new SpannableStringBuilder();
end=m.start();
if(end>start){
builder.append(input.substring(start, end));
start=builder.length();
}
String g=m.group();
builder.append(g.substring(1, g.length()-1).trim()).append("元");
//我这边是一套代码多个项目 t2代表项目1 t4代表项目2 类似这样 根据项目的主体颜色选择对应的文字颜色
if(BuildConfig.FLAVOR_TYPE.equals("t2")||BuildConfig.FLAVOR_TYPE.equals("t4")){
/**
蓝色
*/
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#0083E5")),
start, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
else if(BuildConfig.FLAVOR_TYPE.equals("t7")){
/**
绿色
*/
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#1b9b3e")),
start, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}else {
/**
红色
*/
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#e10130")),
start, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
start=m.end();
}
if(builder==null) content=input;
else{
if(start<input.length()) builder.append(input.substring(start));
content=builder;
}
}
return content;
}
}
调用方法,从后台拿到返回的数据:
msg=mData.get(mIndex);
if(msg instanceof HotNews) msg=((HotNews)msg).getContent();
vNews.setText(msg instanceof CharSequence? (CharSequence)msg: Objects.toString(msg, null));
分割线======================================================
TextView中设置某段字体颜色:
tv_title.setText("支付界MCC查询");
tv_title.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/black_simplified.TTF"));
SpannableStringBuilder builder = new SpannableStringBuilder(tv_title.getText().toString());
//ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor("#FF9900"));
builder.setSpan(redSpan, 3, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//把下标3~6的字符设置为橙色
tv_title.setText(builder);
效果图:
tv_title.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/black_simplified.TTF"));这里是设置自定义的字体,只要把自定义字体的ttf文件放在assets下就可以了:
分割线======================================================
把字符中的数字设置为想要的颜色并加粗显示:
/**
* 把字符中的数字设置为想要的颜色并变大1.2倍
* @param str
* @return
*/
public static SpannableStringBuilder setNumColor(String str) {
SpannableStringBuilder style = new SpannableStringBuilder(str);
for (int i = 0; i < str.length(); i++) {
char a = str.charAt(i);
if (a >= '0' && a <= '9') {
style.setSpan(new ForegroundColorSpan(Color.parseColor("#0083E5")), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new RelativeSizeSpan(1.2f), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return style;
}
使用方法:
textView.setText(setNumColor("该商品12.34元"));