2012-12-21 34 views
0

我想從textview的文本中加粗多個單詞。 我有數組中的文字,我想在textview的文本中加粗。 如何通過SpannableStringBuilder做到這一點?如何在android中編輯textview中的多個單詞

我的數組:

String []數組= { 「生活」, 「幸福」, 「悲傷」, 「眼淚」, 「笑」, 「笑」, 「情感」} ;

我的TextView文本:

生活充滿幸福悲傷眼淚微笑笑聲和 其他情緒但當生活得到你向下,只要它堅強起來,並保持高昂的頭,並相信生活中的一切。

我經過link和其他,但沒有找到任何我想要的解決方案。

TextView text = (TextView) findViewById(R.id.textView1); 
    String[] list = new String[] { "Life","happiness","sadness","tears","smiles","laughter","emotions"};  

    String textData ="Life is filled with happiness, sadness, tears, smiles, laughter and other emotions but when life gets you down, just be strong about it and keep your head up high and have faith in all things in life"; 
    CharSequence cseq = ""; 
    for(int i = 0 ; i < list.length ; i++) 
    { 
      cseq = setSpanBetweenTokens(textData, list[i] , new StyleSpan(Typeface.BOLD_ITALIC)); 
    } 
    text.setText(cseq.toString()); 


public static CharSequence setSpanBetweenTokens(CharSequence text, 
      String token, CharacterStyle... cs) 
     { 
      // Start and end refer to the points where the span will apply 
      int tokenLen = token.length(); 
      int start = text.toString().indexOf(token) + tokenLen; 
      int end = text.toString().indexOf(token, start); 

      if (start > -1 && end > -1) 
      { 
       // Copy the spannable string to a mutable spannable string 
       SpannableStringBuilder ssb = new SpannableStringBuilder(text); 
       for (CharacterStyle c : cs) 
        ssb.setSpan(c, start, end, 0); 

       // Delete the tokens before and after the span 
       ssb.delete(end, end + tokenLen); 
       ssb.delete(start - tokenLen, start); 

       text = ssb; 
      } 

      return text; 
     } 
+0

.setText(array [0] +「填充了」+ array [1] +「」array [2] ...(依此類推) ? – Myth1c

+0

@ Myth1c但我想要做動態。通過SpannableStringBuilder – Smily

回答

2

嘗試爲使用Html.fromHtml:

string[] array = {"Life","happiness","sadness", 
"tears","smiles","laughter","emotions"}; 

String strtemp=""; 
    for(int i=0;i<array.length-2;i++){ 
    if(i<array.length-3) 
     strtemp+="<b>"+array[i]+",<b/>"; 
     else{ 
     strtemp+="<b>"+array[i]+"<b/>"; 
    } 
    } 
    txtview=(TextView)findViewById(R.id.selection); 
    txtview.setText(Html.fromHtml("YOUR_TEXT HERE"+strtemp+ 
      "YOUR_TEXT HERE <b>"+array[array.length-1] 
        +"</b> YOUR_TEXT HERE")); 
+0

謝謝@ρяσѕρєяK但我的textview文本不是靜態的..它的動態..所以我想檢查可用的字符串中的數組值如果可用然後加粗它 – Smily

+0

@ArchanA:你試試看看我的編輯答案是用靜態TextView運行的嗎 –

+0

@ArchanA:如果它不適用於動態TextView n使用SpannableStringBuilder –

1

請使用下面的代碼:

textView.setText(Html.fromHtml("<b>"+array[0]+"</b>"+ "is filled with "+"<b>"+array[1]+"</b>"+","+"<b>"+array[2]+"</b>"+","+"<b>"+array[3]+"</b>"+","+"<b>"+array[4]+"</b>"+","+"<b>"+array[5]+"</b>"+"and other"+"<b>"+array[6]+"</b>"+"but when life gets you down, just be strong about it and keep your head up high and have faith in all things in life.")); 
+0

我不希望你建議我我想要迭代循環直到數組長度,如果發現那個單詞將形成像明智..理解?? – Smily

+0

然後將上述行如果條件 - 檢查,直到數組.length()和array [i]!= null .... –

2

如果我理解正確你的問題,你有一個TextView有一些動態文本在裏面,你想要大膽地說一些話。試試這樣:

Spannable sb = new SpannableString(textFromTextView); 
for(int i = 0 ; i < array.length ; i++) { 
    sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 
     textFromTextView.indexOf(array[i]), 
     array[i].length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 

然後用sb作爲參數調用textview的setText()函數。

相關問題