Hir*_*tel 8 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;">&nbsp;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;">&nbsp;</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>"
Run Code Online (Sandbox Code Playgroud)
到目前为止我做了什么:
TextView textView = (TextView) findViewById(R.id.textView);
String plainText = Html.fromHtml(mHtmlString).toString()
textView.setText(plainText);
Run Code Online (Sandbox Code Playgroud)
运气不好,不适合嵌套HTML.
任何帮助将不胜感激.
Hir*_*tel 33
我给出了答案.
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;">&nbsp;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;">&nbsp;</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>";
Run Code Online (Sandbox Code Playgroud)
在TextView上设置Html文本字符串:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(Html.fromHtml(mHtmlString).toString()));
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
如果你正在寻找从html中删除html标签,那么使用Jsoup(http://jsoup.org)
String textFromHtml = Jsoup.parse(MY_HTML_STRING_HERE).text();
TextView desc = (TextView) dialog.findViewById(R.id.description);
desc.setText(textFromHtml);
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我有用:
Spanned spanned = Html.fromHtml(textWithMarkup);
char[] chars = new char[spanned.length()];
TextUtils.getChars(spanned, 0, spanned.length(), chars, 0);
String plainText = new String(chars);
Run Code Online (Sandbox Code Playgroud)
我将它与 <b> 和 <i> 等简单标签一起使用。没有使用更复杂的 HTML 进行测试。
| 归档时间: |
|
| 查看次数: |
22838 次 |
| 最近记录: |