在我的应用中,我想在带有html标记的文本视图中显示一个字符串。 这将得到字符串:
String mystring = getResources().getString(R.string.Terms_And_Conditions);
除外,不允许使用html字符。我可以在论坛上看到改用getText(),但是它不起作用
mystring = getResources().getText(R.string.Terms_And_Conditions);
我得到了错误
error: incompatible types: CharSequence cannot be converted to String
在所有在文本视图中使用html的示例中,它们都只有一个简单的硬编码字符串,并且效果很好。但是从资源中提取字符串时,它只是吃掉了所有的html,就像html标签不是它们的标签一样,但是它实际上并没有做标记。所有人都说我需要改用getText()。那么,如何使用getText()获取字符串资源,或者如何在字符串资源中显示html?还是只需要直接在Java中对字符串进行硬编码?
谢谢。
如果要使用getText(),则必须这样进行:
String mystring = activity.getResources().getText(R.string.Terms_And_Conditions).toString()
要允许html字符,您可以将资源值包装在<![CDATA[ ... ]]>
例如,如果我要显示字符串<button></button>
:
strings.xml:
<string name="button_button"><![CDATA[<button></button>]]></string>
MainActivity.kt:
val mystring:String = getResources().getText(R.string.button_button).toString();
Log.wtf("mystring", mystring)
日志输出:
E/mystring: <button></button>