2012-02-21 85 views
21

我有我不想用戶,直到一個特定的複選框我的應用程序中選擇可以選擇任何按鈕的單選按鈕組禁用RadioGroup中。如果該複選框取消選中是那麼這將禁用無線電組。我該如何去做這件事。如何,直到複選框被選中

回答

51

真正的竅門是遍歷所有兒童觀看(在這種情況下:CheckBox),並調用它的setEnabled(boolean)

像這樣的東西應該做的伎倆:

//initialize the controls 
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.radioGroup1); 
CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1); 

//set setOnCheckedChangeListener() 
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton checkBox, boolean checked) { 
     //basically, since we will set enabled state to whatever state the checkbox is 
     //therefore, we will only have to setEnabled(checked) 
     for(int i = 0; i < rg1.getChildCount(); i++){ 
      ((RadioButton)rg1.getChildAt(i)).setEnabled(checked); 
     } 
    } 
}); 

//set default to false 
for(int i = 0; i < rg1.getChildCount(); i++){ 
    ((RadioButton)rg1.getChildAt(i)).setEnabled(false); 
} 
+0

很高興知道我所做的「骯髒的黑客攻擊」似乎是實際的做法。 :d – WORMSS 2013-07-11 08:00:36

0

您可以使用您的CheckBox的onCheckedChangeListener和使用方法setEnabled您RadioGroup中。

最良好的祝願, 添

+4

radiogroup.setEnabled(真);不起作用 – Elchin 2012-08-08 10:30:03

+3

對不起,但這對RadioGroup無效。它似乎並沒有傳播下去。 – WORMSS 2013-04-21 21:28:50

-1

根據複選框的狀態採取行動,並設置相應的RadioGroup中。 假設您有一個名爲radiogroup的無線電組,您可以通過

啓用或禁用該radiogroup radiogroup.setEnabled(true);

添加OnCheckedChangeListener()到您的複選框。

+4

radiogroup.setEnabled(true);不起作用 – Elchin 2012-08-08 10:29:28

+2

必須禁用單個RadioButton項目。 – 2015-11-01 16:41:10

0

RadioGroup中不能直接禁止,我們通過單選按鈕必須循環,並將其設置爲啓用假。