gpt4 book ai didi

Android:动态键名 R.string

转载 作者:太空狗 更新时间:2023-10-29 16:28:57 24 4
gpt4 key购买 nike

我有一个 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]

我该怎么做?

弗莱迪

最佳答案

您可以通过名称访问运行时生成的资源:

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);

关于Android:动态键名 R.string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43722367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com