如何在android中的textview中编辑多个单词

时间:2012-12-21 07:48:52

标签: android textview

我想在textview的文本中加粗多个单词。 我在数组中有单词,我想在textview的文本中加粗。 如何通过SpannableStringBuilder来实现?

我的阵列:

  

string [] array =   { “生活”, “快乐”, “悲伤”, “眼泪”, “笑”, “笑”, “情绪”};

我的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;
        }

3 个答案:

答案 0 :(得分: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"));

答案 1 :(得分: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()函数。

答案 2 :(得分: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."));