getString()和getText()有什么区别? [英] What is the difference between getString() and getText()?

查看:330
本文介绍了getString()和getText()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用getString()从我的string.xml
中获取字符串 然而.我刚刚发现getText()方法可以从我的资源中获取HTML标签!

I tried using getString() to get a string from my string.xml
However. I just found the getText() method can fetch HTML tags from my resources!

说:

<string name="mySTring"><b><i>Hello Guys</i></b></string>

令我感到惊讶的是,因为我不得不使用Html.fromHtml()来获取HTML标签-已过时.

it surprised me, because I had to use Html.fromHtml() to fetch the HTML tags - which is deprecated.

这两种方法有何区别?
有什么优点或缺点吗?

Which is the difference between the two methods?
Is there any advantage or disadvantage?

推荐答案

从文档中

对于Resources.getString():

返回与特定资源ID关联的字符串值.它 将会删除所有样式化的文字信息.

Return the string value associated with a particular resource ID. It will be stripped of any styled text information.

对于Resources.getText():

返回与特定资源ID关联的字符串值.这 如果这是一个纯字符串,则返回的对象将是一个String;这将是 其他样式的CharSequence(如果已设置样式).

Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled.

[请注意,Context.getText()Context.getString()在内部从Resources调用方法.]

[Note that Context.getText() and Context.getString() internally calls the methods from Resources.]

文档说getText()保留样式,而getString()保留样式.但是您可以使用任一方法从strings.xml获取带有HTML标记的字符串资源,但是方法不同.

The doc says that getText() retains the styling while the getString() not. But you can use either one to get the string resource with HTML tags from strings.xml, but the way is different.

使用Resources.getText():

strings.xml:

<string name="styled_text">Hello, <b>World</b>!</string>

您可以只调用getText() (请注意,它返回的是CharSequence而不是String,因此它具有样式属性),并将文本设置为TextView.不需要Html.fromHtml().

You can just call getText() (note that it returns a CharSequence not a String, so it has the styling properties) and set the text to TextView. No need for Html.fromHtml().

mTextView.setText(getText(R.string.styled_text));

但是 doc 只能说有限的HTML标记,此方法支持<b>, <i>, <u>之类的字符. 源代码似乎暗示它支持的范围还不止于此:<b>, <i>, <u>, <big>, <small>, <sup>, <sub>, <strike>, <li>, <marquee>, <a>, <font> and <annotation>

But the doc says only a limited HTML tags, such as <b>, <i>, <u> are supported by this method. The source code seems to suggest it supports more than that: <b>, <i>, <u>, <big>, <small>, <sup>, <sub>, <strike>, <li>, <marquee>, <a>, <font> and <annotation>

使用Resources.getString():

strings.xml:

<string name="styled_text"><![CDATA[Hello, <b>World</b>!]></string>

您必须将字符串括在CDATA块中,并调用getString将返回带有HTML标签的字符串.在这里,您必须使用Html.fromHtml().

You have to surround your string in a CDATA block and calling getString will return the string with HTML tags. Here you have to use Html.fromHtml().

mTextView.setText(Html.fromHtml( getString(R.string.styled_text)));

不推荐使用

Html.fromHtml(),而推荐使用带有flags参数的新方法.所以像这样使用它:

Html.fromHtml() is deprecated in favor of a new method with flags parameter. So use it like this:

HtmlCompat.fromHtml(getString(R.string.styled_text))

util方法HtmlCompat.fromHtml的实现:

Implementation of the util method HtmlCompat.fromHtml:

public class HtmlCompat {

    public static CharSequence fromHtml(String source) {

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {

            //noinspection deprecation
            return Html.fromHtml(source);

        } else {

            return Html.fromHtml(source, Html.FROM_HTML_MODE_COMPACT);
        }
    }

}

这篇关于getString()和getText()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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