嗨,我每次生成随机数时都有一些以这种方式构建的值数据,我想从值中显示该数字的值 解决方法是什么
<string name="info47">hello world</string>
<string name="info48">hello dolly</string>
int ok=47;
infotextView.setText(getString(R.string.info{ok}));
最佳答案
试试这段代码:
如果在像 MainActivity 这样的 Activity 中,您可以使用它。
@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()));
}
关于java - 如何从android中的值中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44743149/