我有 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);
}
}
如何改进逻辑?
更新
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 管理它 感谢您的帮助! :)
最佳答案
添加到@Nilesh Rathod。您可以使用名为 populateSpinner (int bgCount, int Endcount)
的方法,该方法获取从开始范围到结束所需的 dentry 数量。
private void populateSpinner (int bgCount, int Endcount) {
ArrayList<String> toothNumber = new ArrayList<>();
for (int i = bgCount; i < Endcount; i++) {
toothNumber.add(i);
}
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
在复选框中调用这样的方法OnCheckedChangeListener。
populateSpinner(1, 5);
关于java - 填充微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701971/