gpt4 book ai didi

string - android - For 循环中的 R.string.[variable]?

转载 作者:行者123 更新时间:2023-12-02 22:00:33 24 4
gpt4 key购买 nike

我正在尝试在 for 循环中创建一个 R.string.[variable] 以节省大量代码行。

我试过了,还是不行

在 strings.xml 中,我有这些

<string name="posA1">Position A 1</string>
<string name="posA2">Position A 2</string>
<string name="posA3">Position A 3</string>
...etc

主.java

    for (int j=0; j<10; j++) {
...
string = R.string. + "posA" + j;
...
}

我该怎么做?

最佳答案

数组呢?

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="testArray">
<item>first</item>
<item>second</item>
<item>third</item>
<item>fourth</item>
<item>fifth</item>
</string-array>
</resources>

然后在您的代码中:

String[] myArray = getResources().getStringArray(R.array.testArray);
for(int i = 0; i<myArray.length(); i++) {
String string = myArray[i];
}

关于string - android - For 循环中的 R.string.[variable]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17014340/

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