该应用程序是一个步进序列应用程序,其中包含16个具有8个按钮的单选按钮组。它工作得很好,除了一旦选择了一个按钮组,我就无法将其关闭,除非我使用已创建的清除按钮来清除所有单选按钮组。我想要添加的是一些代码,当再次选择所选单选按钮时,它会像切换开关一样简单地关闭。我尝试使用切换开关,但是那种方法出现了其他问题。以下是两次尝试,但都只让我停止使用按钮。
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
的工作方式,也不是人们预期的。你应该向你的组添加一个像none
这样的RadioButton
。 - codeMagic