我有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 –
哦,好吧好吧。我現在試着改進邏輯。謝了哥們! –