2017-10-12 177 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);