android - 如何在 Android 中将 Html 文本转换为纯文本?

标签 android converter plaintext htmltext

我需要将HTML 文本转换为字符串形式 文本。

String mHtmlString = "<p class="MsoNormal" style="margin-bottom:10.5pt;text-align:justify;line-height: 10.5pt"><b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;">Lorem Ipsum</span></b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> </span><span style="font-family: Arial, sans-serif; font-size: 8.5pt; line-height: 10.5pt; text-align: justify;">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</span></p>"

到目前为止我做了什么:

TextView textView = (TextView) findViewById(R.id.textView);
String plainText = Html.fromHtml(mHtmlString).toString()
textView.setText(plainText);

运气不好,不适用于嵌套的 HTML。

任何帮助将不胜感激。

最佳答案

我给出我的答案。

String mHtmlString = "<p class="MsoNormal" style="margin-bottom:10.5pt;text-align:justify;line-height: 10.5pt"><b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;">Lorem Ipsum</span></b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> </span><span style="font-family: Arial, sans-serif; font-size: 8.5pt; line-height: 10.5pt; text-align: justify;">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</span></p>";

TextView 上设置 Html 文本字符串:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(Html.fromHtml(mHtmlString).toString()));

希望对您有所帮助。

关于android - 如何在 Android 中将 Html 文本转换为纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22573319/

相关文章:

android - 将 PNG 文件放在哪里以及如何从 XML 布局中的 ImageView 引用它们?

java - 信任自签名证书时的 Android 应用程序 SSL 问题

c# - WPF 转换器 : where does the culture come from

python - 如何在Django中将明文密码转换为PBKDF2算法格式?

c# - 在 C# 中格式化纯文本电子邮件中的表格

linux - 请帮助我使用 linux 终端检查主机文件中的重复项

android - android中viewpager的加载顺序

java - 如何设置一个长整数的SeekBar进度? (或替代品)

WPF 数据绑定(bind)和级联转换器?

jsf - 将 JSF 中的日期转换为表中的 "title"属性