本文介绍了如何搜索的字符串和亮点字一个字在android的文本视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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);
 

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

这篇关于如何搜索的字符串和亮点字一个字在android的文本视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:06