开发者

Android - Getting identifier for view using string variable

开发者 https://www.devze.com 2023-04-03 07:44 出处:网络
I have a string variable which is the same as the identifier for a view. I would like to use the R.id.xxx, but obviously I can\'t just replace the xxx开发者_如何学Go with my string as the xxx is actu

I have a string variable which is the same as the identifier for a view.

I would like to use the R.id.xxx, but obviously I can't just replace the xxx开发者_如何学Go with my string as the xxx is actually an int. What can I do to get around this?

Thanks!


Nonsense. The framework obviously discourages that approach but it is possible: just use Java reflection, e.g.,

 java.lang.reflect.Field f = R.id.class.getField(name);
 int id = f == null ? -1 : (Integer)f.get(null);
 // now just use findViewById(id);


String s=getString(R.string.xxx);
0

精彩评论

暂无评论...
验证码 换一张
取 消