How to search for a word in a string and highlight word in a text view in android?

前端 未结 3 820
广开言路
广开言路 2020-12-30 12:46

in my android application i have a string that contains a specific word so i want to display whole string in text view and the specific word should be highlighted.Hope follo

3条回答
  •  旧巷少年郎
    2020-12-30 13:01

    for each word, you can use:

    TextView textView = (TextView)findViewById(R.id.mytextview01);
    //use a loop to change text color
    Spannable WordtoSpan = new SpannableString("partial colored text");        
    WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(WordtoSpan);
    

提交回复
热议问题