该应用程序是一个步进音序器应用程序,每个组中有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);

}

}

});

2013-04-05

A B

+0

这通常不是如何'RadioButtons'工作,而不是人们期望的。您应该为您的群组添加一个'无'的'RadioButton' –

2013-04-05 14:45:45

+0

为什么不给每个组添加一个OFF按钮? –

2013-04-05 14:43:14

+0

我可以做到这一点,但因为它是单声道的,我只想在每个组中选择一个 –

2013-04-05 14:47:46

更多推荐