gpt4 book ai didi

java - 如何在不删除格式设置的情况下将 Html 转换为字符串?

转载 作者:行者123 更新时间:2023-12-01 12:00:24 25 4
gpt4 key购买 nike

我正在尝试获取一个文本文档,将其拆分,然后搜索结果。如果结果匹配,则文本将被替换为等效文本,只是格式化(粗体、下划线等)。这是我的代码:

String[] result = conversionText.split("//n");

for (int a = 0; a < result.length; a++) {


result[a] = result[a].replaceAll("important", " \n" + Html.fromHtml("<u>IMPORTANT</u>"));

verifyTextView.setText(Arrays.toString(result));
}

我尝试获取字符串资源,但格式与上面所示相同,但​​问题仍然存在。将文本放入字符串时如何保留文本的格式?

最佳答案

更新:只需阅读 TextView#setText(CharSequence text) 引用即可。其中指出:

Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files.

这解释了为什么你不能只是做这样的事情......

verifyTextView.setText("<p>Hello, world!</p>");

但是下面的代码应该可以工作:

String[] result = conversionText.split("//n");

for (int a = 0; a < result.length; a++) {
result[a] = result[a].replaceAll("important", " <br><u>IMPORTANT</u>");
}

verifyTextView.setText(Html.fromHtml(Arrays.toString(result)));

关于java - 如何在不删除格式设置的情况下将 Html 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017715/

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