Android編程實現(xiàn)TextView部分顏色變動的方法
更新時間:2015年12月12日 10:29:28 作者:lee0oo0
這篇文章主要介紹了Android編程實現(xiàn)TextView部分顏色變動的方法,實例分析了TextView設(shè)置指定位置的背景色與字體顏色的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了Android編程實現(xiàn)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); //設(shè)置指定位置textview的背景顏色 style.setSpan(new ForegroundColorSpan(Color.RED),list.get(i),list.get(i)+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //設(shè)置指定位置文字的顏色 } 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程序設(shè)計有所幫助。
您可能感興趣的文章:
- Android TextView字體顏色設(shè)置方法小結(jié)
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android使用selector修改TextView中字體顏色和背景色的方法
- Android編程實現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)
- Android編程設(shè)置TextView顏色setTextColor用法實例
- Android TextView設(shè)置不同的顏色字體
- Android編程實現(xiàn)TextView部分顏色變動的方法
- Android Textview實現(xiàn)顏色漸變滾動效果
- Android 實現(xiàn)不同字體顏色的TextView實現(xiàn)代碼
- Android中TextView實現(xiàn)分段顯示不同顏色的字符串
相關(guān)文章
Android中使用findViewByMe提升組件查找效率
本文主要介紹了Android中使用findViewByMe提升組件查找效率的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Android實戰(zhàn)教程第四十三篇之上拉加載與下拉刷新
這篇文章主要為大家詳細(xì)介紹了Android實戰(zhàn)教程第四十三篇之上拉加載與下拉刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11