有沒有一種方法可以在android中設置RadioGroup的選定索引,而不是循環遍歷子單選按鈕並選擇檢查所選索引處的單選按鈕?設置Android RadioGroup的選定索引
注意:我在運行時填充單選按鈕組。
有沒有一種方法可以在android中設置RadioGroup的選定索引,而不是循環遍歷子單選按鈕並選擇檢查所選索引處的單選按鈕?設置Android RadioGroup的選定索引
注意:我在運行時填充單選按鈕組。
如果您的收音機組是在佈局xml文件中定義的,則可以爲每個按鈕分配一個ID。然後你只需檢查這樣
radioGroup.check(R.id.myButtonId);
如果您創建單選按鈕組編程按鈕(我什至不知道你是如何做到這一點...),你可能需要考慮創建一個特殊的佈局xml文件只是對於收音機組,以便您可以將R.id. * id分配給按鈕。
如果您想查看以索引設置單選按鈕組,請參閱下面的答案。
提問說:「設置選定的索引」,這裏是如何通過索引設置:
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
........
附加信息:看來,谷歌要你使用ID而不是索引,因爲使用ID更有缺陷。例如,如果您的RadioGroup中有另一個UI元素,或者另一個開發人員重新排列了RadioButton,則索引可能會更改,而不是您的預期。但是如果你是唯一的開發者,那就完全沒問題。
您可以從收音機組中執行findViewById。
((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);`
薩瓦什的答案是正確的:
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
但要知道,一個radioGroup中可以包含的觀點比其他單選按鈕 - 類似這樣的例子,其中包括每個選擇下一個淡淡的線。
<RadioGroup
android:id="@+id/radioKb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/kb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="Onscreen - ABC" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="@+id/kb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="Onscreen - Qwerty" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="@+id/kb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="Standard softkey" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="@+id/kb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="Physical keyboard" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
</RadioGroup>
在這種情況下,例如使用1的索引會產生錯誤。索引1處的項目是第一個分隔線 - 不是radioButton。本例中的單選按鈕是在索引0,2,4,6。
這爲我工作,我通過
private void createRadioButton() {
RadioButton[] rb = new RadioButton[5];
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1.0f);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
for (int ID = 0; ID < 5; ID++) {
rb[ID] = new RadioButton(this);
rb[ID].setLayoutParams(layoutParams);
rb[ID].setText("Button_Text");
radioGroup.addView(rb[ID]); //the RadioButtons are added to the radioGroup instead of the layout
}
}
現在使用檢查按鈕動態創建單選按鈕,
int radio_button_Id = radioGroup.getChildAt(index).getId();
radioGroup.check(radio_button_Id);
嗨,創建它們使用程式設計此代碼:對(INT I = 0; I
2012-03-23 16:11:38
由於您將它們的id設置爲lookupTypes中的索引,因此您可以將該索引用作'check'方法的參數。 – jjm 2012-03-23 16:20:40
這個答案的字面意思就是再次解釋提問者已經提出的問題 - 另一個答案應該是被接受的答案,因爲它正確地說明了如何通過索引進行選擇。 – 2015-03-13 08:14:06