【发布时间】:2016-07-23 13:15:55
【问题描述】:
我有这段代码旨在将字符串从 Spinner 项转换为整数,以便在应用程序的另一部分使用。当我在 switch case 中有常量字符串时,这很有效,但后来我想将这些硬编码的字符串移动到 Strings.xml 资源中以将它们与逻辑分开。这就是我遇到麻烦的地方,因为 Java 希望字符串保持不变。
我试图使字符串最终化,但没有任何区别。所以我的问题是,是否有可能以某种方式从资源常量中生成字符串,以使它们在 switch case 中可用,就像下面的代码 sn-p 中的那样?
public int getPositionFromText(String text) {
// The string representations of the different score methods.
String[] scoreOptions = getResources().getStringArray(R.array.scoreOptions);
final String three = scoreOptions[0];
final String four = scoreOptions[1];
final String five = scoreOptions[2];
switch(text) {
case three:
return 0;
case four:
return 1;
case five:
return 2;
default:
return 0;
}
}
【问题讨论】:
-
您需要 编译时间常数 用于您的案例标签
标签: java android string constants