gpt4 book ai didi

android - 从android中的文本文件中搜索文本

转载 作者:行者123 更新时间:2023-11-29 18:19:10 27 4
gpt4 key购买 nike

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

提前致谢

克里希纳库玛 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com