Java 填充微调器
我有4个复选框,当我勾选1、2或更多时。我想用值填充微调器,如果选中复选框1,则填充1-5;如果选中复选框2,则填充6-10,等等。。我有这个逻辑Java 填充微调器,java,android,android-spinner,android-checkbox,Java,Android,Android Spinner,Android Checkbox,我有4个复选框,当我勾选1、2或更多时。我想用值填充微调器,如果选中复选框1,则填充1-5;如果选中复选框2,则填充6-10,等等。。我有这个逻辑 public void populateSpinnerToothNumber() { if (cbQuadrant1.isChecked()) { ArrayList<String> toothNumber = new ArrayList<>(); toothNumber.add("1"
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(){
如果(cbQuadrant1.isChecked()){
ArrayList齿数=新的ArrayList();
编号。添加(“1”);
编号。添加(“2”);
编号。添加(“3”);
编号。添加(“4”);
编号。添加(“5”);
ArrayAdapter stringArrayAdapter=新的ArrayAdapter(此,R.layout.support\u simple\u spinner\u dropdown\u项,齿数);
喷丝头齿数。设置适配器(stringArrayAdapter);
}else if(cbQuadrant2.isChecked()){
}else if(cbQuadrant3.isChecked()){
}else if(cbQuadrant4.isChecked()){
}else if(cbQuadrant1.isChecked()&&cbQuadrant2.isChecked()){
ArrayList齿数=新的ArrayList();
编号。添加(“1”);
编号。添加(“2”);
编号。添加(“3”);
编号。添加(“4”);
编号。添加(“5”);
编号。添加(“6”);
编号。添加(“7”);
编号。添加(“8”);
编号。添加(“9”);
编号。添加(“10”);
ArrayAdapter stringArrayAdapter=新的ArrayAdapter(此,R.layout.support\u simple\u spinner\u dropdown\u项,齿数);
喷丝头齿数。设置适配器(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);
}
public void populateSpinnerToothNumber(){
最终ArrayList齿数=新ArrayList();
cbQuadrant1.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮复合按钮,布尔b){
对于(int x=1;x您应该使用
复合按钮的选中状态更改时要调用的回调的接口定义
示例代码
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (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);
}
}
}
);
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (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);
}
}
}
);
checkBox.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
ArrayList齿数=新的ArrayList();
编号。添加(“1”);
编号。添加(“2”);
编号。添加(“3”);
编号。添加(“4”);
编号。添加(“5”);
ArrayAdapter stringArrayAdapter=新的ArrayAdapter(此,R.layout.support\u simple\u spinner\u dropdown\u项,齿数);
喷丝头齿数。设置适配器(stringArrayAdapter);
}
}
}
);
checkBox2.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
ArrayList齿数=新的ArrayList();
编号。添加(“1”);
编号。添加(“2”);
编号。添加(“3”);
编号。添加(“4”);
编号。添加(“5”);
编号。添加(“6”);
编号。添加(“7”);
编号。添加(“8”);
编号。添加(“9”);
编号。添加(“10”);
ArrayAdapter stringArrayAdapter=新的ArrayAdapter(此,R.layout.support\u simple\u spinner\u dropdown\u项,齿数);
喷丝头齿数。设置适配器(stringArrayAdapter);
}
}
}
);
添加到@Nilesh Rathod。您可以使用一个名为populateSpinner(int-bgCount,int-Endcount)
的方法,该方法从开始到结束接受所需的齿数
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);
}
private void populateSpinner(int-bgCount,int-Endcount){
ArrayList齿数=新的ArrayList();
for(int i=bgCount;i
在复选框OnCheckedChangeListener中这样调用方法。
populateSpinner(1,5);
如果勾选了复选框1和2怎么办?或者如果勾选了复选框1和3怎么办?你必须管理所有复选框更改的侦听器,因为我上面的ans只是一段代码来指导你现在你可以根据你的要求管理所有复选框更改的侦听器@KyleAnthonyDizonOh,好的。我马上尝试改进逻辑。谢谢伙计!非常欢迎@KyleAnthonyDizon很高兴能帮助你让我知道如果有任何疑问