2017-10-12 133 views
2

我有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

你应该使用OnCheckedChangeListener

接口定义回调时,复合按钮的选中状态改为被调用。

示例代码

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); 
               } 
              } 
             } 
    ); 
+0

如果选中复选框1和2,该怎么办?或者如果选中了复选框1和3,该怎么办? –

+0

你必须管理所有复选框更改监听器,因为我的上述答案只是代码片段引导你现在你可以管理所有复选框更改监听器根据您的要求@KyleAnthonyDizon –

+0

哦,好吧好吧。我现在试着改进逻辑。谢了哥们! –

2

添加到@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); 
} 

呼叫在您的复选框OnCheckedChangeListener这样的方法。 populateSpinner (1, 5);