我有一个strings.xml文件:
<string name="wind_direction_n">North</string>
<string name="wind_direction_nne">North-Northeast</string>
<string name="wind_direction_ne">Northeast</string>
<string name="wind_direction_ene">East-Northeast</string>
<string name="wind_direction_e">East</string>
<string name="wind_direction_ese">East-Southeast</string>
<string name="wind_direction_se">Southeast</string>
<string name="wind_direction_sse">South-Souteast</string>
<string name="wind_direction_s">South</string>
<string name="wind_direction_ssw">South-Southwest</string>
<string name="wind_direction_sw">Southwest</string>
<string name="wind_direction_wsw">West-Southwest</string>
<string name="wind_direction_w">West</string>
<string name="wind_direction_wnw">West-Norhwest</string>
<string name="wind_direction_nw">Northwest</string>
<string name="wind_direction_nnw">North-Northwest</string>
我需要这样的事情:
String direction = "nn";
String message = R.string["wind_direction" + direction]
我该怎么做?
弗雷迪
答案 0 :(得分:4)
您可以按名称访问运行时生成的资源:
String direction = "nn";
String fullName = "wind_direction" + direction;
// Get the identifier of the resource by its name.
@StringRes int resId = getResources().getIdentifier(fullName, "string", getPackageName());
// Use the value of the resource.
String message = getString(resId);