在EditText更改后更改Spinner条目

时间:2015-07-02 07:21:48

标签: android android-edittext spinner populate

我想在更改EditText后更改我的Spinner条目: 我有4个EditText:

- edit1
- edit2
- edit3
- edit4

我有2个微调器条目数组:

- spinner1
- spinner2

规则: 如果我在edit1和edit2中写,我得到spinner1; 如果我在edit3和edit4中写,我有spinner2; 如果我在edit1,edit2,edit3和endit4中写,我有spinner1。

我不能只完成1个EditText,第2个,2个持续,或4个EditText。

我的微调器在strings.xml中:

<string-array name="dureeRemboursement">
    <item>- Choisir -</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
    <item>13</item>
    <item>14</item>
    <item>15</item>
    <item>16</item>
    <item>17</item>
    <item>18</item>
    <item>19</item>
    <item>20</item>
    <item>21</item>
    <item>22</item>
    <item>23</item>
    <item>24</item>
    <item>25</item>
</string-array>

<string-array name="dureeRemboursementcc">
    <item>- Choisir -</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
</string-array>

我尝试了两种方法来填充我的微调器:

editCapitauxRestants.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            ArrayAdapter<CharSequence> adapter0 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursementcc, android.R.layout.simple_spinner_item);
            adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursement, android.R.layout.simple_spinner_item);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            if (editMensualitesTotales.getText().length() > 0 && editCapitauxRestants.getText().length() > 0) {
                spinnerDureeRemboursement.setAdapter(adapter1);
            } else {
                spinnerDureeRemboursement.setAdapter(adapter0);
            }
        }
    }
});


spinnerDureeRemboursement.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        ArrayAdapter<CharSequence> adapter0 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursementcc, android.R.layout.simple_spinner_item);
        adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursement, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        if (editMensualitesTotales.getText().length() > 0 && editCapitauxRestants.getText().length() > 0) {
            spinnerDureeRemboursement.setAdapter(adapter1);
        }
    }

    public void onNothingSelected(AdapterView<?> arg0) {
    }
});

第一个不起作用因为,当我点击我的微调器时,焦点仍然在我的EditText中,第二个也是因为当我点击Spinner时它会出错:我无法滚动微调器。

的xml:

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinnerDureeRemboursement"
        android:layout_below="@+id/dureeRemboursement"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:entries="@array/dureeRemboursementcc"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingTop="20dp"
        android:paddingBottom="20dp" />

我尝试使用xml中的条目,但没有查看错误是否来自它,但仍然无效。

我认为有两种解决方案: - 用户滚动时退出焦点; - 专注于我的微调器。

编辑:

我在我的片段中:

onTextChanged:

public class DemandeGratuite extends android.support.v4.app.Fragment {

...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //placer ici le code pour connaitre la densite et la resolution de lecran
        View view = inflater.inflate(R.layout.demande_gratuite, container, false);
...


        /*

        editCapitauxRestants.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    ArrayAdapter<CharSequence> adapter0 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursementcc, android.R.layout.simple_spinner_item);
                    adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursement, android.R.layout.simple_spinner_item);
                    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    if (editMensualitesTotales.getText().length() > 0 && editCapitauxRestants.getText().length() > 0) {
                        spinnerDureeRemboursement.setAdapter(adapter1);
                    } else {
                        spinnerDureeRemboursement.setAdapter(adapter0);
                    }
                }
            }
        });

        spinnerDureeRemboursement.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                ArrayAdapter<CharSequence> adapter0 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursementcc, android.R.layout.simple_spinner_item);
                adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursement, android.R.layout.simple_spinner_item);
                adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                if (editMensualitesTotales.getText().length() > 0 && editCapitauxRestants.getText().length() > 0) {
                    spinnerDureeRemboursement.setAdapter(adapter1);
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        */
...

        return view;
    }

    //Apres creation de la vue ont va creer les evenements
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
...
    }


    public void onTextChanged(Editable editable) {
        ArrayAdapter<CharSequence> adapter0 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursementcc, android.R.layout.simple_spinner_item);
        adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursement, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        int mensualitesTotales = editMensualitesTotales.getText().toString().length();
        int capitauxRestants = editCapitauxRestants.getText().toString().length();
        if (mensualitesTotales > 0 && capitauxRestants > 0)
            spinnerDureeRemboursement.setAdapter(adapter1);
        else {
            spinnerDureeRemboursement.setAdapter(adapter0);
        }
    }
}

ANSWER

    Spinner spinnerDureeRemboursement;

    EditText editMensualitesTotales, editCapitauxRestants, editMensualitesTotalescc, editCapitauxRestantscc;

    String stringMensualitesTotales, stringCapitauxRestants, stringMensualitesTotalescc, stringCapitauxRestantscc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //placer ici le code pour connaitre la densite et la resolution de lecran
        View view = inflater.inflate(R.layout.demande_gratuite, container, false);

        spinnerDureeRemboursement = (Spinner) view.findViewById(R.id.spinnerDureeRemboursement);


        editMensualitesTotales = (EditText) view.findViewById(R.id.editMensualitesTotales);
        editCapitauxRestants = (EditText) view.findViewById(R.id.editCapitauxRestants);
        editMensualitesTotalescc = (EditText) view.findViewById(R.id.editMensualitesTotalescc);
        editCapitauxRestantscc = (EditText) view.findViewById(R.id.editCapitauxRestantscc);


        stringMensualitesTotales = editMensualitesTotales.getText().toString();
        stringCapitauxRestants = editCapitauxRestants.getText().toString();
        stringMensualitesTotalescc = editMensualitesTotalescc.getText().toString();
        stringCapitauxRestantscc = editCapitauxRestantscc.getText().toString();

        editCapitauxRestants.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                ArrayAdapter<CharSequence> adapter0 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursementcc, android.R.layout.simple_spinner_item);
                adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.dureeRemboursement, android.R.layout.simple_spinner_item);
                adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                if (editMensualitesTotales.getText().length() > 0 && editCapitauxRestants.getText().length() > 0) {
                    spinnerDureeRemboursement.setAdapter(adapter1);
                } else {
                    spinnerDureeRemboursement.setAdapter(adapter0);
                }
                //stringMensualitesTotales = s.toString();
                //setupSpinner(stringMensualitesTotales, stringCapitauxRestants, stringMensualitesTotalescc, stringCapitauxRestantscc);
            }
        });
}

1 个答案:

答案 0 :(得分:0)

    private String editString1,editString2,editString3,editString4;
    public void onCreate(){
    //Initialise views
    edit1.addTextChangedListener(new TextWatcher() {
          @Override
          public void afterTextChanged(Editable s) {
            editString1 = s.toString();   setupSpinner(editString1,editString2,editString3,editString4)
        })
       //DO the same for other edit texts and save them to appropriate Strings
    }

   public void setupSpinner(String first,String second,String third,String four){
       //Check the strings and set up your spinners appropriately
    }