我将文本文件保留在资产文件夹中,并且可以在活动中的TextView中显示整个文本。但是现在我需要在活动中添加搜索功能,突出显示搜索到的文本。任何人都可以提出任何想法或向我发送代码段
完成这个。

提前致谢

Krishnakumar P

最佳答案

使用以下代码突出显示文本。

    void hightLightText(TextView textView, String searchString){
        try{
//if mydata.txt file is present in assets directory
            InputStream fin = getAssets().open("mydata.txt");
            byte[] buffer = new byte[fin.available()];
            fin.read(buffer);
            String actualdata = new String(buffer);
            String withHighLightedText = actualdata.replaceAll(searchString, "<font color='red'>"+actualdata)+"</font>";
            String styledText = "This is <font color='red'>simple</font>.";
            textView.setText(Html.fromHtml(withHighLightedText), TextView.BufferType.SPANNABLE);
            }catch(Exception ex){

            }

    }

关于android - 从android中的文本文件搜索文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6468107/

10-11 22:43
查看更多