Android编程实现TextView部分颜色变动的方法
程序员文章站
2024-02-11 21:37:40
本文实例讲述了android编程实现textview部分颜色变动的方法。分享给大家供大家参考,具体如下:
public class stringhandleexa...
本文实例讲述了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; } }
希望本文所述对大家android程序设计有所帮助。