如何在TextView Android中解码HTML和转义字符 [英] How to decode Html and escape characters in Textview android

查看:694
本文介绍了如何在TextView Android中解码HTML和转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含一些html字符(例如

I have specific data that contain some html characters such as

   &lt; for < 
   &gt; for > 
   &amp; for &

还有一些\r\n\r\n这样的转义字符,当我尝试使用tv.setText(str);在文本视图中显示数据时,这只会解码转义字符并显示html字符.

And some escape characters like this \r\n\r\n, When i try to display my data in text-view using tv.setText(str); this only decode escape characters and show html characters as it.

当我尝试使用类似Html.fromHtml(str)这样的内容时,请勿解码转义字符.

And when i try to use something like this Html.fromHtml(str) not decode the escape characters.

那么,有什么办法可以解决这个问题?

So, there is any way to handle this issue ?

推荐答案

Html.fromHtml(str).toString();应该可以.我告诉你原因-

Html.fromHtml(str).toString(); should work. I tell you the reason -

您的字符串是Html. Unicode字符存储为编码的字符实体. &#x;符号用于转义unicode字符,以便通过ISO-8859-1进行传输. 网络浏览器会对它们进行解码以显示实际的unicode字符.

Your string is Html. Unicode characters are stored as encoded character entities. The &#x; notation is used to escape unicode characters for transmission over ISO-8859-1. A web browser decodes them to display actual unicode characters.

解码HTML就是将HTML实体解码为Java原始unicode字符.

Decoding HTML is decoding HTML entities to Java raw unicode characters.

例如:

String html = "B &#x0026; This is HTML";
String java = Html.fromHtml(html);
#=> Output: "B \u0026 This is HTML"
String strJava = Html.fromHtml(html).toString();
#=> Output: "B & This is HTML"

这篇关于如何在TextView Android中解码HTML和转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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