简体   繁体   English

根据Android Studio中的字符串值动态添加单选按钮列表?

[英]Dynamically adding radio button list according to string values in Android Studio?

I have seen that this can be done for ListView where you can just grab the string-array values from strings.xml and populate the ListView dynamically depending on the number of listed items.我已经看到这可以为ListView完成,您可以从strings.xml 中获取字符串数组值,并根据列出的项目数量动态填充 ListView。

But I was wondering if the same feat can be done using Radio Buttons and Radio Button Group?但我想知道是否可以使用 Radio Buttons 和 Radio Button Group 来完成同样的壮举?

I've come across this piece of code我遇到了这段代码

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
RadioGroup.LayoutParams rprms;

for(int i=0;i<3;i++){
      RadioButton radioButton = new RadioButton(this);
      radioButton.setText("new"+i);
      radioButton.setId("rbtn"+i);
      rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      rgp.addView(radioButton, rprms);

  }

And was wondering how I can modify the radioButton.setId("rbtn"+i) to grab string-array values from the strings.xml file instead.并且想知道如何修改radioButton.setId("rbtn"+i)以从strings.xml文件中获取字符串数组值。

Use String[] foo_array = getResources().getStringArray(R.array.foo_array);使用String[] foo_array = getResources().getStringArray(R.array.foo_array); to read array from strings.xml.从strings.xml 读取数组。 Then loop through the obtained array.然后循环遍历获得的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM