简体   繁体   English

从 Android 中编码的 EditText 获取源 HTML

[英]Get source HTML from encoded EditText in Android

I'm displaying formatted text in an EditText as such:我在 EditText 中显示格式化文本,如下所示:

String msg = <b>Some text here</b>;
EditText txtMsg = (EditText) findViewById(R.id.txtMessage);
txtMsg.setText(Html.fromHtml(msg));

That displays in the EditText as " Some text here. " Then, later, I'd like to pull the text from that EditText complete with the tags.这在 EditText 中显示为“这里有一些文本。 ”然后,稍后,我想从该 EditText 中提取带有标签的文本。 However, when I use:但是,当我使用:

txtMsg.getText().toString();

It just gives me "Some text here" with no formatting.它只是给我“这里有一些文字”,没有格式。

Any thoughts on this?对此有何想法?

尝试从 EditText 获取 HTML 格式的文本:

String msgtext=Html.toHtml(txtMsg.getText());

Html.toHtml is deprecated in Java and Android N Html.toHtml 在 Java 和 Android N 中已弃用

You better use你最好用

HtmlCompat.toHtml(
                        txtMsg.text,
                        HtmlCompat.TO_HTML_PARAGRAPH_LINES_INDIVIDUAL
                    )

for more info https://developer.android.com/reference/androidx/core/text/HtmlCompat.html ? 了解更多信息https://developer.android.com/reference/androidx/core/text/HtmlCompat.html

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

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