Higlight特定的词在文本视图 [英] Higlight Particular Word in Text View

查看:309
本文介绍了Higlight特定的词在文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序中,我正在寻找一个Perticular字的文字view.I有一个编辑的文字,一个文本视图和一个按钮,当我进入编辑文本的任何单词,点击按钮,它给了我线的位置和字的位置,该行从整个文本文件,我还追加文本文件的包含文本视图...现在我的问题是,我可以强调,所有的字是有由编辑文本输入的文本视图。 ?如果我能比请告诉我该怎么做it..if任何一个有它的想法?

I am creating one application in which I am searching for a Perticular word in text view.I have one edit text,one text view and one button,when I am entering any word in edit text and clicking on button it gives me the line position and position of word in that line from entire text file...I have append text file's contain in text view...now my question is can I highlight that all word which is there in text view entered by edit text.?if I can than please tell me how to do it..if any one have idea of it?

推荐答案

您也可以做到这一点通过使用的 Spannable ,这是方便,因为你知道这个词的位置:

You can also do it by using a Spannable, which is convenient as you know the position of the word:

    SpannableString res = new SpannableString(entireString);
    res.setSpan(new ForegroundColorSpan(color), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);

其中, entireString 是这个词来突出显示字符串存在,颜色是要在突出的颜色文有,启动这个词的位置和结束是字结束(启动+ word.length ())。

Where entireString is the string in which the word to highlight exists, color is the color you want the hightlighted text to have, start is the position of the word and end is where the word ends (start+word.length()).

该SpannableString资源可以被应用到你的TextView就像一个普通的字符串:

The SpannableString res can then be applied to your textview like a regular string:

textView.setText(res);

注意:如果您想要的文字的背景下获得的颜色,而不是文本本身,使用了 BackgroundColorSpan 而不是 ForegroundColorSpan

Note: If you want the background of the text to get a color rather than the text itself, use a BackgroundColorSpan instead of a ForegroundColorSpan.

编辑: 在你的情况下,它会是这样的(你必须保存linecount和indexfound,当你阅读整个文本值):

In your case it would be something like this (you will have to save the value of linecount and indexfound for when you read the entire text):

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

这篇关于Higlight特定的词在文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆