简体   繁体   English

如何在不嵌套特定标签的情况下将 EditText 转换为 html

[英]How can I convert EditText to html without nesting specific tags

I use this code to convert the text of edittext to html我使用此代码将edittext的文本转换为html

    String html = Html.toHtml(edittext.getText);

But it keeps nesting tags.但它保持嵌套标签。 That's not my problem.那不是我的问题。 But I don't want to nest some specific tags like <img>但我不想嵌套一些特定的标签,比如<img>

For example, this is the text of EditText:例如,这是 EditText 的文本:

Hello how are you?
[Here's an image.]

Expected result:预期结果:

    <p dir="ltr">Hello how are you</p> 
    <img src="path/to/image">

But it gives me this:但它给了我这个:

    <p dir="ltr">Hello how are you<br> <img src="path/to/image"> </p>

I don't want that <img> tag gets nested in the the <p> .我不希望<img>标记嵌套在<p>中。 Because I couldn't show the image later.因为我后来无法显示图像。 I just need to avoid only <img> gets nested.我只需要避免<img>被嵌套。 Not other tags.不是其他标签。

You can split the string before converting it by delimiter:您可以在通过分隔符转换之前拆分字符串:

String getHtml = "<p dir="ltr">Hello how are you</p> <img src="path/to/image">"; 
String[] splitElements = getHtml.split("</p>");

And after that, you can pass both strings and convert those.之后,您可以传递两个字符串并进行转换。 They will be separate and non-nested.它们将是独立的且非嵌套的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM