textview和

1,中间混杂不同颜色的字体
public static void setSize(TextView tv, String text, int one, int tow, String colors, int sizea, int three, int four, String colors1, int sizea1) {
    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);

}

具体使用:

gongxi.setText( "恭喜 " );
String string =
str + " 获得 " + list .get( count ).getTitle();
setSize( gongxi, string, 0 , str .length(),
       
"#5eccff" , 13 , str .length(), string.length(),
        "#646464" , 13 );
2, 动态更改字体的大小

关键代码 
- setTextSize(TypedValue.COMPLEX_UNIT_PX,15); //22像素 
- setTextSize(TypedValue.COMPLEX_UNIT_SP,15); //22SP 
- setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);//22DIP

3,文字加粗
android:textStyle="bold" 

4,动态设置view,textview,imageview的颜色背景
Android 代码中使用Color工具类 方法parseColor解析
view.setBackgroundColor(Color.parseColor("#365663"));

rb_jjss .setTextColor(Color.parseColor( "#00ac8b" ));
yuanquan .setImageResource(R.drawable. lvquan );
5,   设置为数字   android :password= "true"
6,设置输入的只是数字   android :inputType= "number"
7,添加下划线
tv_mima_yazheng.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
8,textview末尾省略号  最长为18位,单行
<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; 
}

猜你喜欢

转载自blog.csdn.net/qq_30299243/article/details/81047267
今日推荐