我的 Assets 文件夹中有一个 .txt 文件,其中包含大量 HTML...
我将其调用为一个字符串,使用 JSoup 库删除一些 HTML 标签,并将该字符串设置为 TextView 的文本:
InputStream is;
TextView tv = (TextView)findViewById(R.id.tv);
String html;
try {
is = getAssets().open("html.txt");
html = convertStreamToString(is);
} catch (Exception e) {
e.printStackTrace();
}
Document doc = Jsoup.parse(html);
Elements links = doc.select("a[href]");
for (Element link : links) {
doc.select("a").unwrap();
}
doc.select("title").remove();
tv.setText(fromHtml(html));
它会导致 TextView 显示出非常大且漂亮的格式化 HTML 代码
当用户触摸该 TextView 中的任何单词时,我想更改该单词的背景颜色,对其进行标记,并存储该标记以便在用户重新启动应用程序时再次标记...我怎样才能实现这一点?
编辑:
另外,我如何将 Spannable
和 Html.fromHtml()
一起使用?
最佳答案
这篇文章可以帮助您获取单词的索引。 OnClick Position in String in TextView 。保存单词的起始索引。
从索引到单词结尾,使用spannablestring突出显示单词。 How to search for a word in a string and highlight word in a text view in android?
当您再次加载文本时,您可以对存储的单词索引执行相同的操作。
关于java - 如何在TextView中标记单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42755339/