【发布时间】:2023-03-29 18:43:01
【问题描述】:
该应用程序是一个步进音序器应用程序,具有 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);
}
}
});
【问题讨论】:
-
为什么不给每个组添加一个OFF按钮?
-
RadioButtons通常不是这样工作的,也不是人们所期望的。您应该将RadioButton像none添加到您的组中 -
我可以这样做,但由于它是单声道的,我希望每个组中只选择一个
-
为每个组设置一个清晰的按钮会起作用,但不是很实用
-
无论如何,您只会在每个组中选择一个。这就是单选按钮的工作方式。从设计的角度来看,您的问题是您实际上没有使用任何按钮作为额外条件,您无法切换。