HTML.fromHtml 换行符消失 [英] HTML.fromHtml line breaks disappearing

查看:31
本文介绍了HTML.fromHtml 换行符消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 EditText 框中获取跨区文本,并使用 HTML.toHtml 将其转换为 HTML 标记字符串.这工作正常.我已经验证该字符串是正确的,并且在适当的位置包含一个 <br>.但是,当我必须将标记字符串转换回跨区文本以使用 HTML.fromHtml 填充 TextView 或 EditText 时,<br> (或多个,如果它们存在)在末尾第一段消失.这意味着如果用户输入带有多个换行符的文本并希望保留该格式,它就会丢失.

I am taking Spanned Text from an EditText box and converting it to a HTML tagged string using HTML.toHtml. This works fine. I have verified that the string is correct and contains a <br> in the appropriate location. However, when I got to convert the tagged string back to a spanned text to populate a TextView or EditText using HTML.fromHtml the <br> (or multiple ones if they are present) at the end of the first paragraph disappear. This means that if a users entered text with multiple line breaks and wanted to keep that formatting it gets lost.

我附上了一张图片来帮助说明这一点.第一个 EditText 是用户输入,它下面的 TextView 是它上面的 EditText 的 HTML.tohtml 结果,它下面的 EditText 使用 HTML.fromHtml 使用它上面的 TextView 中的字符串填充.如您所见,换行符已消失,多余的行也已消失.此外,当第二个编辑文本的跨区文本通过 HTML.toHtml 运行时,它现在会生成一个不同的 HTML 标记字符串.

I attached a picture to help illustrate this. The first EditText is the user input, the TextView Below it is the HTML.tohtml result of the EditText above it, the EditText below it is populated using HTML.fromHtml using the string in the TextView above it. As you can see the line breaks have disappeared and so have the extra lines. Furthermore, when the spanned text of the second edit text is run through the HTML.toHtml it now produces a different HTML tagged String.

我希望能够从第一个 EditText 中获取带有 HTML 标记的字符串并填充其他 TextViews 或 EditTexts 而不会丢失换行符和格式.

I would like to be able to take the HTML tagged String from the first EditText and populate other TextViews or EditTexts without losing line breaks and formatting.

推荐答案

我也有这个问题,我找不到简单的转换"或类似的解决方案.请注意一些重要的事情,当用户按下回车"时,java 会产生特殊字符 但在 HTML 中没有这样的换行格式.它是 <br/>.

I also had this problem and I could not find an easy "transform" or something alike solution. Note something important, when the user presses "enter" java produces the special character but in HTML there is no such format for line breaking. It is the <br />.

所以我所做的就是用另一种 HTML 格式替换纯文本中的一些特定 CharSequence.就我而言,只有输入"字符,所以没有那么混乱.

So what I have done was to replace some specific CharSequences, from the plain text, by the alternative HTML format. In my case there was only the "enter" character so it was not that messy.

这篇关于HTML.fromHtml 换行符消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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