2014-11-04 28 views
1

作爲我的應用程序的一部分,我必須在radiogroup中創建四個radioButton並獲得單擊的單選按鈕。我編寫了下面的代碼,但是我的selectId屬性給出了當選擇第一個選項時的值2131034181和當第二個選項被選中時2131034182等等,當點擊3和4時單位數值的變化。爲什麼這樣?RadioGroup的getCheckedRadioButtonId()返回2131034181/2/3/4

int selectId=rbg.getCheckedRadioButtonId(); 
RadioButton selected= (RadioButton) rbg.findViewById(selectId); 

String selected_user = (String) selected.getText(); 
+0

我認爲你正在尋找選中的單選按鈕的指數?如果是,請檢查:http://stackoverflow.com/a/6441097/1911652 – Atul 2016-05-01 10:56:31

回答

0

要獲得radiobutton,使用findViewByIdcontext而不是radiogroup,所以改變

RadioButton selected= (RadioButton) rbg.findViewById(selectId); 
String selected_user = (String) selected.getText(); 

RadioButton selected = (RadioButton)findViewById(selectId); 
    String selected_user = selected.getText().toString(); 
+0

我的問題是與getCheckedRadioButtonId()的返回值,代碼工作正常,但返回整數值2131034181而不是1. – user3747888 2014-11-04 15:01:37

+0

@ user3747888 Do你想獲得單選按鈕索引或單選按鈕的ID? – 2014-11-04 17:03:40