EditText.setError doesnt show Error text and only

2019-07-05 12:46发布

问题:

I have a simple validation in my app, here I am using four EdtiText.I am showing error when EditText loses focus but, the problem is on losing focus EditText only shows icon without error message. I have tried using requestFocus() method and can now see the error but the problem is.. now my form shows two cursors and even if the first field is not valid and showing errors, whatever I am typing goes into second EdtiText. Can anybody help me to fix this? Thanks.

Here is my xml file -

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="first name"
android:id="@+id/edt_first_name"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="last name"
    android:id="@+id/edt_last_name"
    android:singleLine="true"
    android:layout_below="@+id/edt_first_name"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="email"
    android:singleLine="true"
    android:id="@+id/edt_email"
    android:layout_below="@+id/edt_last_name"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="password"
    android:singleLine="true"
    android:imeOptions="actionNext"
    android:id="@+id/edt_password"
    android:layout_below="@+id/edt_email"/>

and here is my main file where i am cheking validations.

firstname.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {

            if (!hasFocus) {
                if (!Validate(firstname.getText().toString())) {

                } else {
                    firstname.setFocusable(true);
                    firstname.setError("not valid");
                }
            }else{
                firstname.setError(null);
            }

        }
    });

回答1:

Use TextInputLayout for material EditText like :

<android.support.design.widget.TextInputLayout
            android:id="@+id/input_application_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:minHeight="50dp"
            android:theme="@style/MyTextInputLayout"
            app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">

            <EditText
                android:id="@+id/application_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_application_name"
                android:imeOptions="actionNext"
                android:inputType="text" />

        </android.support.design.widget.TextInputLayout>

and in your Activity

TextInputLayout til = (TextInputLayout) findViewById(R.id.input_application_name);
EditText applicationNameEdt = (EditText) findViewById(R.id.application_name);
til.setErrorEnabled(true);
til.setError("You need to enter a name");