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

2023-09-05 04:06:23 作者:旧味衬衫.

在我的Andr​​oid应用程序,我有一个包含特定的词,所以我想显示在文本视图和特定的词应该是highlighted.Hope下面的图片会给你一个想法,整串的字符串。

我已经使用下列code要做到这一点,但它不工作。

code:

con是我的字符串,groupNameContent是文本字段。

 con.replaceAll(arrGroupelements [groupPosition] [5],&所述;字体颜色='#CA278C'>中+ arrGroupelements [groupPosition] [5] +&其中; /字体取代。);
groupNameContent.setText(Html.fromHtml(CON));
 

解决方案

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

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

//使用循环来改变文字颜色
    Spannable WordtoSpan =新SpannableString(局部彩色文本);
    WordtoSpan.setSpan(新ForegroundColorSpan(Color.BLUE),2,4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(WordtoSpan);
 
如何在Android的自定义列表视图中使用搜索功能

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