文本视图中的特殊词

Aam*_*han 6 android textview

我正在创建一个应用程序,我在其中搜索文本视图中的特定单词.我有一个编辑文本,一个文本视图和一个按钮,当我在编辑文本中输入任何单词并单击按钮时,它给出了行位置和整个文本文件中该行的单词位置...我在文本视图中附加了文本文件...现在我的问题是我可以突出显示编辑文本输入的文本视图中的所有单词.如果我可以告诉我怎么做...如果有人有这个想法吗?

Jav*_*ave 7

您也可以使用Spannable来完成它,这很方便,因为您知道单词的位置:

    SpannableString res = new SpannableString(entireString);
    res.setSpan(new ForegroundColorSpan(color), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

entireString要突出显示的单词所在的字符串在哪里,color是您希望高亮文本所具有的颜色,start是单词的位置,并且end是单词结束的位置(start + word.length()).

然后,SpannableString res可以像常规字符串一样应用于textview:

textView.setText(res);
Run Code Online (Sandbox Code Playgroud)

注意:如果您希望文本的背景颜色而不是文本本身,请使用a BackgroundColorSpan而不是a ForegroundColorSpan.

编辑: 在你的情况下,它将是这样的(当你读取整个文本时,你将不得不保存linecount和indexfound的值):

for(String test="", int currentLine=0; test!=null; test=br2.readLine(), currentLine++){
    if(currentLine==linecount){
        SpannableString res = new SpannableString(test);
        res.setSpan(new ForegroundColorSpan(0xFFFF0000), indexfound, indexfound+textword.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    tv.append("\n"+"    "+test);
}
Run Code Online (Sandbox Code Playgroud)