我在strings.xml中有一串字符串,所有字符串的id/名称都有一个模式(e0、e1、e2、e3、e4.)根据用户选择的数字,我想显示23个字符串中的一个。例如,如果用户选择了数字6,那么我希望显示字符串"e6“。如果不使用超长开关语句,我如何做到这一点?
我在用IntelliJ的想法
谢谢你的帮助,你所有的答案都很有用。
发布于 2013-11-14 04:36:01
可以使用字符串数组
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
例子:
保存在res/value/strings.xml s.xml中的XML文件:
<?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>此应用程序代码检索字符串数组:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);发布于 2013-11-14 06:42:09
以Michael为例,我会做这样的事情:
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.https://stackoverflow.com/questions/19969615
复制相似问题