简体   繁体   中英

how to edit multiple word in textview in android

I want to bold multiple words from textview's text. I am having words in array that i want to bold in textview's text. how to do it by SpannableStringBuilder??

My array:

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

My Textview text:

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.

I go through link and other but not found any solution which i want.

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

try as using 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"));

If I understood your question correctly, you have a TextView that has some dynamic text in it, and you want to bold certain words in it. Try something like this:

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

then call the setText() function of your textview with sb as parameter.

请使用以下代码:

 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."));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM