清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
本工具类功能:
给TextView设置部分大小
给TextView设置部分颜色
给TextView设置下划线
半角转换为全角
去除特殊字符或将所有中文标号替换为英文标号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /** * Created by peng on 2015/06/16. * 文本框工具类 */ public class TextViewUtil { //给TextView设置部分大小 public static void setPartialSize(TextView tv, int start, int end, int textSize) { String s = tv.getText().toString(); Spannable spannable = new SpannableString(s); spannable.setSpan( new AbsoluteSizeSpan(textSize), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(spannable); } //给TextView设置部分颜色 public static void setPartialColor(TextView tv, int start, int end, int textColor) { String s = tv.getText().toString(); Spannable spannable = new SpannableString(s); spannable.setSpan( new ForegroundColorSpan(textColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(spannable); } //给TextView设置下划线 public static void setUnderLine(TextView tv) { if (tv.getText() != null ) { String udata = tv.getText().toString(); SpannableString content = new SpannableString(udata); content.setSpan( new UnderlineSpan(), 0 , udata.length(), 0 ); tv.setText(content); content.setSpan( new UnderlineSpan(), 0 , udata.length(), 0 ); } else { tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); } } //取消TextView的置下划线 public static void clearUnderLine(TextView tv) { tv.getPaint().setFlags( 0 ); } //半角转换为全角 public static String ToDBC(String input) { char [] c = input.toCharArray(); for ( int i = 0 ; i < c.length; i++) { if (c[i] == 12288 ) { c[i] = ( char ) 32 ; continue ; } if (c[i] > 65280 && c[i] < 65375 ) c[i] = ( char ) (c[i] - 65248 ); } return new String(c); } //去除特殊字符或将所有中文标号替换为英文标号 public static String replaceCharacter(String str) { str = str.replaceAll( "【" , "[" ).replaceAll( "】" , "]" ) .replaceAll( "!" , "!" ).replaceAll( ":" , ":" ).replaceAll( "(" , "(" ).replaceAll( "(" , ")" ); // 替换中文标号 String regEx = "[『』]" ; // 清除掉特殊字符 Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll( "" ).trim(); } } |