我有丑陋的代码片段的一部分:
if (name.equals("video"))
getString(R.string.video);
else if (name.equals("music"))
getString(R.string.music)
else if (name.equals("picture"))
getString(R.string.picture);
else if (name.equals("text"))
getString(R.string.text);
else if (name.equals("apk"))
getString(R.string.apk);
是否可以简化为类似 getString(R.string.#{ name })
您可以按名称获取标识符:
int id = getResources().getIdentifier(name, "string", getPackageName());
String str = getResources().getString(id);
唯一的问题:
else if (name.equals("text"))
getString(R.string.picture);
您可以在获取标识符之前先进行if语句检查。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句