首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何自动访问strings.xml?(Android)

如何自动访问strings.xml?(Android)
EN

Stack Overflow用户
提问于 2013-11-14 04:34:01
回答 2查看 123关注 0票数 0

我在strings.xml中有一串字符串,所有字符串的id/名称都有一个模式(e0、e1、e2、e3、e4.)根据用户选择的数字,我想显示23个字符串中的一个。例如,如果用户选择了数字6,那么我希望显示字符串"e6“。如果不使用超长开关语句,我如何做到这一点?

我在用IntelliJ的想法

谢谢你的帮助,你所有的答案都很有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-14 04:36:01

可以使用字符串数组

http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

例子:

保存在res/value/strings.xml s.xml中的XML文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

此应用程序代码检索字符串数组:

代码语言:javascript
复制
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
票数 7
EN

Stack Overflow用户

发布于 2013-11-14 06:42:09

以Michael为例,我会做这样的事情:

代码语言:javascript
复制
Int num; //the number the user chooses

TextView tv = (TextView) findViewById(R.id.textviewID);

tv.setText(planets[num - 1]); //arrays start at zero so you would have to subtract one to get the right string. And also so you dont reference a nonexistent variable in the array.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19969615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档