SetError along with onClick for EditText

The normal error is displayed using the method setError():

Screenshot # 1

Problem:

Screenshot # 2

So, I have another one EditTextin the same dialog box with OnClickListenerto display the dialog DatePicker. When I setError()display a red warning icon, and when I click on this icon, the event is still processed OnClickon EditTextand DatePicker, therefore, I cannot view the error message.

What I want: if I click on the icon, it should show an error message, and if I go beyond the icon, it should show DatePicker.

+4
source share
4 answers

, , 2 . HAVE focus ( , . , , - EditText TextInputLayout :

<android.support.design.widget.TextInputLayout
            android:id="@+id/birthDateInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_birth_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/birth_date_hint"
                android:inputType="date" />
        </android.support.design.widget.TextInputLayout>

, , TextInputLayout :

birthDateInputLayout.setErrorEnabled(true);
birthDateInputLayout.setError("This field cannot be empty.");

. , EditText, -.

, : enter image description here

+2

- , onclickListener. ..

if(((EditText)view).getError() == null) {
//Handle your click for showing picker
}
0

null.any

mEditText.setError(null);//removes error
mEditText.clearFocus();    //clear focus from edit text
0

,

onClick(View v){
    if(editText.getError() != null ){
       editText.requestFocus(); // to show error message
    }else{
        // open date picker dialog here or do you stuff here 
    }

,

0

Source: https://habr.com/ru/post/1685303/


All Articles