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

如何在android中编辑textview中的多个单词,android,textview,Android,Textview,我想从textview的文本中加粗多个单词。 我想在textview的文本中加粗数组中的单词。 如何通过SpannableStringBuilder实现此功能 我的阵列: 字符串[]数组= {“生活”、“幸福”、“悲伤”、“眼泪”、“微笑”、“笑声”、“情感”} 我的文本查看文本: 生活充满了幸福,悲伤,眼泪,微笑,笑声和 其他情绪但当生活让你沮丧时,要坚强 保持高昂的头,对生活中的一切充满信心 我经历了很多次,但没有找到任何我想要的解决方案 TextView text = (TextV

我想从textview的文本中加粗多个单词。 我想在textview的文本中加粗数组中的单词。 如何通过SpannableStringBuilder实现此功能

我的阵列:

字符串[]数组= {“生活”、“幸福”、“悲伤”、“眼泪”、“微笑”、“笑声”、“情感”}

我的文本查看文本:

生活充满了幸福悲伤眼泪微笑笑声和 其他情绪但当生活让你沮丧时,要坚强 保持高昂的头,对生活中的一切充满信心

我经历了很多次,但没有找到任何我想要的解决方案

    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;
        }
TextView text=(TextView)findViewById(R.id.textView1);
字符串[]列表=新字符串[]{“生活”、“幸福”、“悲伤”、“眼泪”、“微笑”、“笑声”、“情感”};
String textData=“生活中充满了快乐、悲伤、眼泪、微笑、笑声和其他情绪,但当生活让你沮丧时,要坚强,昂首挺胸,对生活中的一切充满信心”;
CharSequence cseq=“”;
for(int i=0;i-1和结束>-1)
{
//将可扩展字符串复制到可变可扩展字符串
SpannableStringBuilder ssb=新的SpannableStringBuilder(文本);
用于(字符样式c:cs)
ssb.固定盘(c,开始,结束,0);
//删除跨度前后的标记
ssb.delete(结束,结束+令牌);
ssb.delete(start-tokenLen,start);
text=ssb;
}
返回文本;
}

请使用以下代码:

 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."));
textView.setText(Html.fromHtml(“+array[0]+”“+”填充“+”+array[1]+”、“+”+array[2]+”、“+”+array[3]+”、“+”+array[4]+“+”、“+”+array[5]+“+”和其他“+”+array[6]+“+”)但当生活让你沮丧时,你要坚强起来,昂首挺胸,对生活中的一切充满信心);

请使用以下代码:

 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."));
textView.setText(Html.fromHtml(“+array[0]+”“+”填充“+”+array[1]+”、“+”+array[2]+”、“+”+array[3]+”、“+”+array[4]+“+”、“+”+array[5]+“+”和其他“+”+array[6]+“+”)但当生活让你沮丧时,你要坚强起来,昂首挺胸,对生活中的一切充满信心);

尝试使用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"));
string[]数组={“生活”、“幸福”、“悲伤”,
“眼泪”、“微笑”、“笑声”、“情绪”};
字符串strtemp=“”;

对于(int i=0;i尝试使用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"));
string[]数组={“生活”、“幸福”、“悲伤”,
“眼泪”、“微笑”、“笑声”、“情绪”};
字符串strtemp=“”;

对于(int i=0;i如果我正确理解了您的问题,您有一个
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);
}
Spannable sb=新SpannableString(textFromTextView);
for(int i=0;i

然后用sb作为参数调用textview的setText()函数。

如果我正确理解了你的问题,你有一个
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);
}
Spannable sb=新SpannableString(textFromTextView);
for(int i=0;i

然后用sb作为参数调用textview的setText()函数。

.setText(数组[0]+”用“+数组[1]+”数组[2]…(等等)?@Myth1c但我想动态执行。通过SpannableStringBuilder.setText(数组[0]+”用“+数组[1]+”数组[2]…(等等)填充)?@Myth1c,但我想动态地做。通过spannablestringbuilder,我不想像你建议的那样,我想像迭代循环一样直到数组长度,如果在其中找到的单词会像wise一样格式化。明白吗?然后将上面的行放入if条件中-检查直到数组。length()和数组[i]!=null…我不想像你建议的那样,我想像迭代循环一样直到数组长度,如果在其中找到的单词将像wise一样格式化..明白吗?然后将上面的行放入if条件中-检查直到数组.length()和数组[I]!=null…谢谢@ρцσѕρєK,但我的文本视图文本不是静态的。它是动态的。所以我想检查字符串中的数组值是否可用,如果可用,请将其加粗。@ArchanA:您试过了吗?查看我的编辑答案,它正在使用静态运行TextView@ArchanA:如果无法与动态文本视图一起使用,则使用SpannableStringBuilder@ArchanA:
not获得完美的输出
意味着你能分享一些相关的代码吗try@ArchanA:使用当前代码得到的结果是什么?是否可以显示当前输出的图像?谢谢@ρ∑ѕρєK,但我的文本视图文本不是静态的。它是动态的。因此,我想检查字符串中的数组值是否可用加粗。@ArchanA:你试过了吗?看到我的编辑答案了吗