且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何搜索的字符串和亮点字一个字在android的文本视图?

更新时间:2023-11-15 10:24:28

对于每个字,你可以使用:

 的TextView TextView的=(的TextView)findViewById(R.id.mytextview01);

//使用循环来改变文字颜色
    Spannable WordtoSpan =新SpannableString(局部彩色文本);
    WordtoSpan.setSpan(新ForegroundColorSpan(Color.BLUE),2,4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(WordtoSpan);
 

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 following image will give you an idea.

I have used following code to do this but its not working.

CODE:

con is my string and groupNameContent is the text field.

con.replaceAll(arrGroupelements[groupPosition][5],"<font color='#CA278C'>"+arrGroupelements[groupPosition][5]+"</font>.");
groupNameContent.setText(Html.fromHtml(con));

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