根据 Android Studio 中的字符串值动态添加单选按钮列表?
Dynamically adding radio button list according to string values in Android Studio?
我看到可以为 ListView 完成此操作,您可以从 strings.xml[=25= 中获取字符串数组值] 并根据列出的项目数 动态地 填充 ListView。
但我想知道是否可以使用单选按钮和单选按钮组来完成同样的壮举?
我遇到过这段代码
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);
}
想知道如何修改 radioButton.setId("rbtn"+i)
以从 strings.xml 文件中获取字符串数组值。
使用 String[] foo_array = getResources().getStringArray(R.array.foo_array);
从 strings.xml 读取数组。然后循环得到数组。
我看到可以为 ListView 完成此操作,您可以从 strings.xml[=25= 中获取字符串数组值] 并根据列出的项目数 动态地 填充 ListView。
但我想知道是否可以使用单选按钮和单选按钮组来完成同样的壮举?
我遇到过这段代码
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);
}
想知道如何修改 radioButton.setId("rbtn"+i)
以从 strings.xml 文件中获取字符串数组值。
使用 String[] foo_array = getResources().getStringArray(R.array.foo_array);
从 strings.xml 读取数组。然后循环得到数组。