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; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值