SpannableStringBuilder 和 SpannableString

EditText: 
        一般用於顯示文字,但有時候也須要在文字中夾雜一些圖片,好比QQ中就能夠使用表情圖片,又好比須要的文字高亮顯示等等,如何在android中也作到這樣呢? 
記得android中有個android.text包,這裏提供了對文本的強大的處理功能。 
添加圖片主要用SpannableString和ImageSpan類: html

 1      Drawable drawable = getResources().getDrawable(id);  
 2      drawable.setBounds(0, 0,
 3               drawable.getIntrinsicWidth(), 
 4               drawable.getIntrinsicHeight());  
 5         //須要處理的文本,[smile]是須要被替代的文本  
 6         SpannableString spannable = new SpannableString(getText().toString()
 7                                                     +"[smile]");  
 8         //要讓圖片替代指定的文字就要用ImageSpan  
 9         ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);  
10         //開始替換,注意第2和第3個參數表示從哪裏開始替換到哪裏替換結束(start和end)  
11         //最後一個參數相似數學中的集合,[5,12)表示從5到12,包括5但不包括12  
12         spannable.setSpan(span, getText().length(),getText().length()
13                    +"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);    
14         setText(spannable);  

 有的時候可能只想用一個textview來表示文字,但文字裏確定有分不一樣內容的東西,須要用不一樣的顏色、樣式來表示. 
        這個時候,就須要用到SpannableString對象來處理。 android

/**
  * new StrikethroughSpan() //設置刪除線
  * new URLSpan("http://www.baidu.com")//http超連接
  * new URLSpan("tel:4155551212")//電話超連接
  */
mTextView = (TextView)findViewById(R.id.test);
SpannableString tSS = new SpannableString(「SpannableString學習中」);
tSS.setSpan(new BackgroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  //紅色高亮
tSS.setSpan(new UnderlineSpan(), 15, 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    //下劃線
tSS.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //斜體
mTextView.setText(tSS);

關鍵方法: 
public void setSpan (Object what, int start, int end, int flags) 
start是起始位置,不管中英文,都算一個。從0開始計算起。end是結束位置,因此處理的文字,包含開始位置,但不包含結束位置。 學習

 

將須要的文字高亮顯示: 字體

1 public void highlight(int start,int end){  
2         SpannableStringBuilder spannable=
3               new SpannableStringBuilder(getText().toString());//用於可變字符串  
4         ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);  
5         spannable.setSpan(span, start, 
6                          end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
7         setText(spannable);  
8 }  

加下劃線:ui

1 public void underline(int start,int end){  
2         SpannableStringBuilder spannable=
3                   new SpannableStringBuilder(getText().toString());  
4         CharacterStyle span=new UnderlineSpan();  
5         spannable.setSpan(span, start, 
6                   end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
7         setText(spannable);  
8 }  

組合運用:spa

1 SpannableStringBuilder spannable=
2         new SpannableStringBuilder(getText().toString());  
3         CharacterStyle span_1=new StyleSpan(android.graphics.Typeface.ITALIC);  
4         CharacterStyle span_2=new ForegroundColorSpan(Color.RED);  
5         spannable.setSpan(span_1, start, 
6                       end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
7         spannable.setSpan(span_2, start, 
8                       end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
9         setText(spannable);  

案例:帶有\n換行符的字符串均可以用此方法顯示2種顏色 code

 1  /** 
 2    * 帶有\n換行符的字符串均可以用此方法顯示2種顏色 
 3    * @param text 
 4    * @param color1 
 5    * @param color2 
 6    * @return 
 7    */  
 8    public SpannableStringBuilder highlight(String text,int color1,int color2,int fontSize){  
 9        SpannableStringBuilder spannable=new SpannableStringBuilder(text);//用於可變字符串  
10        CharacterStyle span_0=null,span_1=null,span_2;  
11        int end=text.indexOf("\n");  
12        if(end==-1){//若是沒有換行符就使用第一種顏色顯示  
13            span_0=new ForegroundColorSpan(color1);  
14            spannable.setSpan(span_0, 0, 
15                            text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
16        }else{  
17            span_0=new ForegroundColorSpan(color1);  
18            span_1=new ForegroundColorSpan(color2);  
19            spannable.setSpan(span_0, 0, 
20                            end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
21            spannable.setSpan(span_1, end+1, 
22                            text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
23            span_2=new AbsoluteSizeSpan(fontSize);//字體大小  
24            spannable.setSpan(span_2, end+1, 
25                            text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
26         }  
27        return spannable;  
28    }

 

字體背景顏色(BackgroundColorSpan)

1 SpannableString spanString = new SpannableString("歡迎光臨Harvic的博客");  
2 BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);  
3 spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
4 editText.setText(spanString);  

 

以上實際都是Html.fromHtml(html)的代碼實現形式。 
============================================================================= 

TextView: (方法與EditView相似) 
如何讓一個TextView中的關鍵字高亮顯示? htm

 1     /** 
 2      * 關鍵字高亮顯示 
 3      * @param target 須要高亮的關鍵字 
 4      */  
 5     public void highlight(String target){  
 6         String temp=getText().toString();  
 7         SpannableStringBuilder spannable = new SpannableStringBuilder(temp);  
 8         CharacterStyle span=null;  
 9           
10         Pattern p = Pattern.compile(target);  
11         Matcher m = p.matcher(temp);  
12         while (m.find()) {  
13             span = new ForegroundColorSpan(Color.RED);//須要重複!
14             //span = new ImageSpan(drawable,ImageSpan.XX);//設置如今圖片
15             spannable.setSpan(span, m.start(),  m.end(),
16                               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
17         }  
18         setText(spannable);  
19     }
相關文章
相關標籤/搜索