码迷,mamicode.com
首页 > 移动开发 > 详细

Android之TextView部分颜色变动

时间:2014-05-06 14:39:57      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   class   code   java   

bubuko.com,布布扣
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;
    }
    
}
bubuko.com,布布扣

 

Android之TextView部分颜色变动,布布扣,bubuko.com

Android之TextView部分颜色变动

标签:android   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/lee0oo0/p/3710476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!