我有以下问题:
我创建了一个字符序列并能够命名 4 个单元。然而,我宁愿使用我的 XML 文件中的字符串来进行本地化。有什么办法可以实现吗?
final CharSequence[] choices =
//want to add strings here i.e. R.strings.lemonade
{"Coke", "Pepsi" , "Sprite" , "Seven Up" };
builderType.setSingleChoiceItems( choices, selected, new OnClickListener()
{.......
错误信息:
Type mismatch: cannot convert from int to CharSequence
最佳答案
还有一个 AlertDialog.Builder.setSingleChoiceItems()
的重载它接受一个字符串项目数组的 int
资源 ID。将以下内容放入 res/values
的 xml 中,例如strings.xml
:
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
然后您可以将其用作:
builderType.setSingleChoiceItems(R.array.choices, selected, new OnClickListener(), ...
对于一般情况,您还可以按照@Egor 的建议使用Resources.getStringArray()
加载字符串数组资源。
关于android - 将资源字符串添加到字符序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16234777/