嗨我每次生成随机数时都会以这种方式构造一些数据,并且我想从值中显示该数字的值 什么是解决方案
<string name="info47">hello world</string>
<string name="info48">hello dolly</string>
int ok=47;
infotextView.setText(getString(R.string.info{ok}));
答案 0 :(得分:1)
试试这段代码:
如果在MainActivity等活动中,您可以使用此功能。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int ok=47;
String name="info"+ok;
TextView infotextView= (TextView) findViewById(R.id.textView);
infotextView.setText(getStringByName(this,name));
}
public static String getStringByName(Context context, String name) {
Resources res = context.getResources();
return res.getString(res.getIdentifier(name, "string", context.getPackageName()));
}
答案 1 :(得分:1)
试试这样。
<string-array name="info">
<item>One</item>
<item>Two</item>
<item>Three</item>
<item>Four</item>
<item>Five</item>
</string-array>
String [] infoArray = getResources().getStringArray(R.array.info);
int randomNumber = 1; // Place your random number here
String value = infoArray[randomNumber];