本文参考自: 原文地址
SpannableString sp = new SpannableString(text);
DisplayMetrics ff = MyApp.getInstance().getResources().getDisplayMetrics();
int size = ( int ) TypedValue.applyDimension(TypedValue. COMPLEX_UNIT_SP , sizea, ff);
sp.setSpan( new AbsoluteSizeSpan(size), one, tow, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
sp.setSpan( new ForegroundColorSpan(Color.parseColor(colors)), one, tow, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
int size2 = ( int ) TypedValue.applyDimension(TypedValue. COMPLEX_UNIT_SP , sizea1, ff);
sp.setSpan( new AbsoluteSizeSpan(size2), three, four, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
sp.setSpan( new ForegroundColorSpan(Color.parseColor(colors1)), three, four, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
tv.append(sp);
}
具体使用:
String string = str + " 获得 " + list .get( count ).getTitle();
setSize( gongxi, string, 0 , str .length(),
"#5eccff" , 13 , str .length(), string.length(),
关键代码
- setTextSize(TypedValue.COMPLEX_UNIT_PX,15); //22像素
- setTextSize(TypedValue.COMPLEX_UNIT_SP,15); //22SP
- setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);//22DIP
android:textStyle="bold"
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="18"
android:singleLine="true"
android:ellipsize="end" />
9.把电话号码变成星号
(1),获得解析的数据并转换成char【】
str = data.getString("mobile");
char[] ch = str.toCharArray();
(2),把textview赋值一个方法change(ch)
tv_dianhua.setText(change(ch));
(3)change(ch)方法
public String change(char[] ch ){
return ch[0] + "" + ch[1] + "" + ch[2] + "****" + ch[7] + "" +ch[8] + "" + ch[9] + "" + ch[10];
}
10正则表达式判断字符串是否是纯数字
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}