首页 > 代码库 > Android之TextView部分颜色变动
Android之TextView部分颜色变动
public class StringHandleExampleActivity extends Activity { /** Called when the activity is first created. */ private TextView textView; private String tempStr = "abcd12我的中古zx9yu5!f3,,"; private StringBuffer sb; private List<Integer> list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sb = new StringBuffer(); list = new ArrayList<Integer>(); textView = (TextView) findViewById(R.id.textView); for (int i = 0; i < tempStr.length(); i++) { if (matcherReg(String.valueOf(tempStr.charAt(i)))) { list.add(i); } } SpannableStringBuilder style=new SpannableStringBuilder(tempStr); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)+", "); // style.setSpan(new BackgroundColorSpan(Color.RED),list.get(i),list.get(i)+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色 style.setSpan(new ForegroundColorSpan(Color.RED),list.get(i),list.get(i)+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色 } textView.setText(style); } private boolean matcherReg(CharSequence c){ String regEx="[^0-9]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(c.toString()); if (m.matches()) { return false; } return true; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。