該應用程序是一個步進音序器應用程序,每個組中有8個按鈕,共有16個電臺組。除非我使用我創建的清除按鈕來清除所有的radiogroups,否則一旦某個組選擇了按鈕,我無法關閉。我想添加的是一些代碼,說當一個選定的單選按鈕被再次選中時,它就會像切換一樣關閉。我嘗試過使用切換,但隨後出現了其他問題。下面是兩次嘗試但都只是使用按鈕Android單選按鈕取消選中
final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton D1 = (RadioButton)findViewById(R.id.RadioButtonD1);
Button D1 = (Button)findViewById(R.id.RadioButtonD1);
D1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PdBase.sendFloat("D1", 74);
int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
if(D1 != null) // This will be null if none of the radio buttons are selected
radioGroup1.clearCheck();
PdBase.sendFloat("D1", 0);
}
});
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);
lC1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
RadioButton lC1 = (RadioButton) findViewById(R.id.RadioButtonlowC1);
if (selectedTypeId == -1){
PdBase.sendFloat("lC1", 72);
}
else if (selectedTypeId == R.id.RadioButtonlowC1) {
radioGroup1.clearCheck();
PdBase.sendFloat("lC1", 0);
}
}
});
這通常不是如何'RadioButtons'工作,而不是人們期望的。您應該爲您的羣組添加一個'無'的'RadioButton' – codeMagic 2013-04-05 14:45:45
爲什麼不給每個組添加一個OFF按鈕? – 2013-04-05 14:43:14
我可以做到這一點,但因爲它是單聲道的,我只想在每個組中選擇一個 – 2013-04-05 14:47:46