我有一个从0到10的Spinner。例如,当我们选择5时,我们得到第5个EditText。我想在验证按钮后检查第5个EditText是否为空。
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants1"
android:layout_below="@+id/ageEnfants"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Premier enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants2"
android:layout_below="@+id/editAgeEnfants1"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Deuxième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants3"
android:layout_below="@+id/editAgeEnfants2"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Troisième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants4"
android:layout_below="@+id/editAgeEnfants3"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Quatrième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants5"
android:layout_below="@+id/editAgeEnfants4"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Cinquième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants6"
android:layout_below="@+id/editAgeEnfants5"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Sixième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants7"
android:layout_below="@+id/editAgeEnfants6"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Septième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants8"
android:layout_below="@+id/editAgeEnfants7"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Huitième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants9"
android:layout_below="@+id/editAgeEnfants8"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Neuvième enfant"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="2"
android:inputType="number"
android:id="@+id/editAgeEnfants10"
android:layout_below="@+id/editAgeEnfants9"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:background="#EBEBEB"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:hint="Dixième enfant"/>
我设置了明显的消失:
editAgeEnfants1.setVisibility(View.GONE);
editAgeEnfants2.setVisibility(View.GONE);
editAgeEnfants3.setVisibility(View.GONE);
editAgeEnfants4.setVisibility(View.GONE);
editAgeEnfants5.setVisibility(View.GONE);
editAgeEnfants6.setVisibility(View.GONE);
editAgeEnfants7.setVisibility(View.GONE);
editAgeEnfants8.setVisibility(View.GONE);
editAgeEnfants9.setVisibility(View.GONE);
editAgeEnfants10.setVisibility(View.GONE);
微调器:
<Spinner
android:id="@+id/spinnerEnfantsCharge"
style="@style/spinner_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/enfantsCharge"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:entries="@array/enfantsCharge"
android:paddingBottom="10dp"
android:paddingTop="10dp" />
的strings.xml
<string-array name="enfantsCharge">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</string-array>
我如何显示EditText:
spinnerEnfantsCharge.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (position == 1) {
ageEnfants.setVisibility(View.VISIBLE);
editAgeEnfants1.setVisibility(View.VISIBLE);
editAgeEnfants2.setVisibility(View.GONE);
editAgeEnfants2.setText("");
editAgeEnfants3.setVisibility(View.GONE);
editAgeEnfants3.setText("");
editAgeEnfants4.setVisibility(View.GONE);
editAgeEnfants4.setText("");
editAgeEnfants5.setVisibility(View.GONE);
editAgeEnfants5.setText("");
editAgeEnfants6.setVisibility(View.GONE);
editAgeEnfants6.setText("");
editAgeEnfants7.setVisibility(View.GONE);
editAgeEnfants7.setText("");
editAgeEnfants8.setVisibility(View.GONE);
editAgeEnfants8.setText("");
editAgeEnfants9.setVisibility(View.GONE);
editAgeEnfants9.setText("");
editAgeEnfants10.setVisibility(View.GONE);
editAgeEnfants10.setText("");
} ... else if (position == 10) {
ageEnfants.setVisibility(View.VISIBLE);
editAgeEnfants1.setVisibility(View.VISIBLE);
editAgeEnfants2.setVisibility(View.VISIBLE);
editAgeEnfants3.setVisibility(View.VISIBLE);
editAgeEnfants4.setVisibility(View.VISIBLE);
editAgeEnfants5.setVisibility(View.VISIBLE);
editAgeEnfants6.setVisibility(View.VISIBLE);
editAgeEnfants7.setVisibility(View.VISIBLE);
editAgeEnfants8.setVisibility(View.VISIBLE);
editAgeEnfants9.setVisibility(View.VISIBLE);
editAgeEnfants10.setVisibility(View.VISIBLE);
} else {
ageEnfants.setVisibility(View.GONE);
editAgeEnfants1.setVisibility(View.GONE);
editAgeEnfants1.setText("");
editAgeEnfants2.setVisibility(View.GONE);
editAgeEnfants2.setText("");
editAgeEnfants3.setVisibility(View.GONE);
editAgeEnfants3.setText("");
editAgeEnfants4.setVisibility(View.GONE);
editAgeEnfants4.setText("");
editAgeEnfants5.setVisibility(View.GONE);
editAgeEnfants5.setText("");
editAgeEnfants6.setVisibility(View.GONE);
editAgeEnfants6.setText("");
editAgeEnfants7.setVisibility(View.GONE);
editAgeEnfants7.setText("");
editAgeEnfants8.setVisibility(View.GONE);
editAgeEnfants8.setText("");
editAgeEnfants9.setVisibility(View.GONE);
editAgeEnfants9.setText("");
editAgeEnfants10.setVisibility(View.GONE);
editAgeEnfants10.setText("");
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
ageEnfants.setVisibility(View.GONE);
editAgeEnfants1.setVisibility(View.GONE);
editAgeEnfants1.setText("");
editAgeEnfants2.setVisibility(View.GONE);
editAgeEnfants2.setText("");
editAgeEnfants3.setVisibility(View.GONE);
editAgeEnfants3.setText("");
editAgeEnfants4.setVisibility(View.GONE);
editAgeEnfants4.setText("");
editAgeEnfants5.setVisibility(View.GONE);
editAgeEnfants5.setText("");
editAgeEnfants6.setVisibility(View.GONE);
editAgeEnfants6.setText("");
editAgeEnfants7.setVisibility(View.GONE);
editAgeEnfants7.setText("");
editAgeEnfants8.setVisibility(View.GONE);
editAgeEnfants8.setText("");
editAgeEnfants9.setVisibility(View.GONE);
editAgeEnfants9.setText("");
editAgeEnfants10.setVisibility(View.GONE);
editAgeEnfants10.setText("");
}
});
在我的验证按钮上,我做了:
else if ((spinnerEnfantsCharge.getSelectedItemPosition() == 1 && editAgeEnfants1.getText().length() == 0) || (spinnerEnfantsCharge.getSelectedItemPosition() == 1 && editAgeEnfants1.getText().length() == 0)) {
ageEnfants.requestFocus();
ageEnfants.setError("Veuillez entrer l'âge de vos enfants.");
但是如果检查所有条件,代码会很长,我想知道是否有更简单的方法来检查空的EditText?
if ((editAgeEnfants1.getVisibility() == View.VISIBLE && editAgeEnfants1.getText().length() == 0) || (editAgeEnfants2.getVisibility() == View.VISIBLE && editAgeEnfants2.getText().length() == 0) || (editAgeEnfants3.getVisibility() == View.VISIBLE && editAgeEnfants3.getText().length() == 0) || (editAgeEnfants4.getVisibility() == View.VISIBLE && editAgeEnfants4.getText().length() == 0) || (editAgeEnfants5.getVisibility() == View.VISIBLE && editAgeEnfants5.getText().length() == 0) || (editAgeEnfants6.getVisibility() == View.VISIBLE && editAgeEnfants6.getText().length() == 0) || (editAgeEnfants7.getVisibility() == View.VISIBLE && editAgeEnfants7.getText().length() == 0) || (editAgeEnfants8.getVisibility() == View.VISIBLE && editAgeEnfants8.getText().length() == 0) || (editAgeEnfants9.getVisibility() == View.VISIBLE && editAgeEnfants9.getText().length() == 0) || (editAgeEnfants10.getVisibility() == View.VISIBLE && editAgeEnfants10.getText().length() == 0)) {...}
答案 0 :(得分:0)
您可以使用
进行检查editTextName.getText().toString().equals("")
答案 1 :(得分:0)
你可以用以下方式检查:
TextUtils.isEmpty(mEditText.getText()。toString())
注意:为什么不使用带适配器的EditText列表?
您将拥有一个包含X EditText的ArrayList(根据您的微调器编号)并创建一个&#34;验证&#34;解析所有arrayList的methode,检查EditText是否为空。
答案 2 :(得分:0)
您可以将其用作
if((mEditText.getText().toString()).equals("")){
}else{
}