0

我想从 textview 的文本中加粗多个单词。我在数组中有我想在 textview 的文本中加粗的单词。SpannableStringBuilder 如何做到这一点?

我的数组:

string[] array = {"生活","幸福","悲伤","眼泪","微笑","笑声","情绪"};

我的文本视图文本:

生活充满了幸福悲伤泪水微笑欢笑和其他情绪,但是当生活让你失望时,只要坚强起来,昂首挺胸,对生活中的一切充满信心。

我通过链接和其他但没有找到我想要的任何解决方案。

    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;
        }
4

3 回答 3

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"));
于 2012-12-21T07:56:21.893 回答
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() 函数。

于 2012-12-21T08:02:36.527 回答
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."));
于 2012-12-21T07:54:43.257 回答