Android 搜索到的關鍵字改變顏色

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class MyAdapter extends BaseAdapter {  
  2.   
  3.     @Override  
  4.     public int getCount() {  
  5.         return mAnchors.size();  
  6.     }  
  7.   
  8.     @Override  
  9.     public Object getItem(int position) {  
  10.         return mAnchors.get(position);  
  11.     }  
  12.   
  13.     @Override  
  14.     public long getItemId(int position) {  
  15.         return position;  
  16.     }  
  17.   
  18.     @Override  
  19.     public View getView(int arg0, View arg1, ViewGroup arg2) {  
  20.         LayoutInflater mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  21.         View mView = mInflater.inflate(R.layout.cell_anchor, arg2, false);  
  22.         ImageView mImage = (ImageView) mView  
  23.                 .findViewById(R.id.search_anchor_image);  
  24.         TextView mTextName = (TextView) mView  
  25.                 .findViewById(R.id.search_anchor_name);  
  26.         TextView mTextId = (TextView) mView  
  27.                 .findViewById(R.id.search_anchor_id);  
  28.           
  29.         int chageTextColor;  
  30.         ForegroundColorSpan redSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue));  
  31.         LiveRoom anchor = mAnchors.get(arg0);  
  32.   
  33.         SpannableStringBuilder builder = new SpannableStringBuilder(  
  34.                 anchor.anchorName);  
  35.         chageTextColor = anchor.anchorName.indexOf(mSearchText);  
  36.         if (chageTextColor != -1) {  
  37.             builder.setSpan(redSpan, chageTextColor, chageTextColor  
  38.                     + mSearchText.length(),  
  39.                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  40.             mTextName.setText(builder);  
  41.         } else  
  42.             mTextName.setText(anchor.anchorName);  
  43.   
  44.         SpannableStringBuilder builder1 = new SpannableStringBuilder(  
  45.                 String.valueOf(anchor.anchorId));  
  46.         chageTextColor = String.valueOf(anchor.anchorId).indexOf(  
  47.                 mSearchText);  
  48.         if (chageTextColor != -1) {  
  49.             builder1.setSpan(redSpan, chageTextColor, chageTextColor  
  50.                     + mSearchText.length(),  
  51.                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  52.             mTextId.setText(builder1);  
  53.         } else  
  54.             mTextId.setText(String.valueOf(anchor.anchorId));  
  55.         return mView;  
  56.     }  
  57.   
  58. }  
發佈了30 篇原創文章 · 獲贊 36 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章