在 EditText 更改后更改 Spinner 条目
Change Spinner entries after EditText changes
我想在更改我的 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>
我尝试了 2 种方法来填充微调器:
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 中,第二个也是因为当我单击微调器时出现错误:我无法滚动微调器。
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);
}
}
}
回答
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);
}
});
}
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
}
我想在更改我的 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>
我尝试了 2 种方法来填充微调器:
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 中,第二个也是因为当我单击微调器时出现错误:我无法滚动微调器。
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);
}
}
}
回答
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);
}
});
}
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
}