[英]Dynamically adding radio button list according to string values in Android Studio?
我已經看到這可以為ListView完成,您可以從strings.xml 中獲取字符串數組值,並根據列出的項目數量動態填充 ListView。
但我想知道是否可以使用 Radio Buttons 和 Radio Button Group 來完成同樣的壯舉?
我遇到了這段代碼
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 讀取數組。 然后循環遍歷獲得的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.