字符串中数字的颜色的改变

/**
* 设置字符串中的数字显示红色
* @param str
*/
public static void setNumberTextColor(String str,TextView textView,Context c,int resourcesID){
char[] s = str.toCharArray();
SpannableString ss = new SpannableString(str);
for (int i = 0; i < s.length; i++){
if (isNum(String.valueOf(s[i]))){
ss.setSpan(new ForegroundColorSpan(c.getResources().getColor(resourcesID)), i, i+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
textView.setText(ss);
}

/**
* 判断是否是数字
* @param str
* @return
*/
private static boolean isNum(String str) {
try {

new BigDecimal(str);
return true;
} catch (Exception e) {
return false;
}
}
posted @ 2017-05-04 11:50  xiao_pengs  阅读(1364)  评论(0)    收藏  举报