从android资源中选择一个值

时间:2022-10-21 15:43:46

In javascript, we can conditionally select a value from a object using a variable as,

在javascript中,我们可以使用变量as来有条件地从对象中选择一个值,

var myObj = {"a": "Val1", "b": "Val2", "c": "Val3"};
var key = "a";
var val = myObj[key];

What is the similar method to select the value from a string resource file in android using variable string name?

使用变量字符串名称从android中的字符串资源文件中选择值的类似方法是什么?

We can select a string as R.string.mystring in android and from a java public class

我们可以在android和java公共类中选择一个字符串作为R.string.mystring

public class myarray {
    public static final String test = "Test";    
    public myarray(){    
    }
}

as String val = myarray.test; but we wanted a method something similar to above javascript.

as String val = myarray.test;但是我们想要一个类似于上面的javascript的方法。

1 个解决方案

#1


2  

A translation of your javascript code is the following:

您的javascript代码的翻译如下:

HashMap<String, String> myObj = new HashMap<String, String>();
myObj.put("a","Val1");
myObj.put("b","Val2");
myObj.put("c","Val3");
String key = "a";
String value = myObj.get(key);

#1


2  

A translation of your javascript code is the following:

您的javascript代码的翻译如下:

HashMap<String, String> myObj = new HashMap<String, String>();
myObj.put("a","Val1");
myObj.put("b","Val2");
myObj.put("c","Val3");
String key = "a";
String value = myObj.get(key);