我有4个复选框,当我检查一个,两个或更多的勾号。我想用值填充我的微调框,如果复选框1被选中,则为1-5;如果复选框2被选中,则为6-10等。我在这里有这个逻辑。填充微调框
public void populateSpinnerToothNumber() {
if (cbQuadrant1.isChecked()) {
ArrayList<String> toothNumber = new ArrayList<>();
toothNumber.add("1");
toothNumber.add("2");
toothNumber.add("3");
toothNumber.add("4");
toothNumber.add("5");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
} else if (cbQuadrant2.isChecked()) {
} else if (cbQuadrant3.isChecked()) {
} else if (cbQuadrant4.isChecked()) {
} else if (cbQuadrant1.isChecked() && cbQuadrant2.isChecked()) {
ArrayList<String> toothNumber = new ArrayList<>();
toothNumber.add("1");
toothNumber.add("2");
toothNumber.add("3");
toothNumber.add("4");
toothNumber.add("5");
toothNumber.add("6");
toothNumber.add("7");
toothNumber.add("8");
toothNumber.add("9");
toothNumber.add("10");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
}
我该如何改进逻辑?
UPDATE
public void populateSpinnerToothNumber() {
final ArrayList<String> toothNumber = new ArrayList<>();
cbQuadrant1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 1; x <= 5; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
cbQuadrant2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 6; x <= 10; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
cbQuadrant3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 11; x <= 15; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
cbQuadrant4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 16; x <= 20; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
这个问题解决了:)与checkbox.setOnCheckedChangeListener由于管理它的帮助! :)
如果选中复选框1和2,该怎么办?或者如果选中了复选框1和3,该怎么办? –
你必须管理所有复选框更改监听器,因为我的上述答案只是代码片段引导你现在你可以管理所有复选框更改监听器根据您的要求@KyleAnthonyDizon –
哦,好吧好吧。我现在试着改进逻辑。谢了哥们! –