TextInputLayout error after enter value into edittext

馋奶兔 提交于 2019-11-29 05:48:50

To illustrate further the answer given by Prithviraj, TextInputLayout does not do the validation itself. It is just a mechanism to show the error or hint. You are responsible for setting/clearing the error. Here is how you can do that. Note that in addition to TextChangedListener, you may also need OnFocusChangeListener to set the error when user jumps to second edit text without doing any modification in the first field.

protected void onCreate(Bundle savedInstanceState) {
        //.....

        edtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                validateEditText(s);
            }
        });

        edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    validateEditText(((EditText) v).getText());
                }
            }
        });
    }

    private void validateEditText(Editable s) {
        if (!TextUtils.isEmpty(s)) {
            layoutEdtPhone.setError(null);
        }
        else{
            layoutEdtPhone.setError(getString(R.string.ui_no_password_toast));
        }
    }
}
fab

You can set layoutEdtPhone.setErrorEnabled(false);

Do

mTextInputLayout.setError(null);

to clear the error message.

A good practice can be a method to check for errors like this:

@Override
public void checkErrorBeforeAction() {

    boolean error = false;

    mTextInputLayout.setError(null);

    if (mEditText.getText().toString().length == 0)) {
        mTextInputLayout.setError("Field empty");
    }
    else if (mEditText.getText().toString().isValid) { // Other condition
        mTextInputLayout.setError("Field is invalid");
    }
    if (!error) {
        // Call action
    }
}

This way it refreshes the error message before setting a new one.

I used @TextChanged of ButterKnife and worked for me, look:

@Bind(R.id.layoutEdtPhone)
TextInputLayout tlayoutEdtPhone;
@Bind(R.id.edtPhone)
EditText edtPhone;

//start ButterKnife (I spent the URL with full description for initilize)

@OnTextChanged(R.id.edtPhone)
public void changedTextOnEditPhone() {
    tlayoutEdtPhone.setError("");
}

If you want know about ButterKnife, I wrote a post with more detail, but it was done in my native language, that is, pt_br. http://blog.alura.com.br/aumentando-a-produtividade-com-butter-knife-no-android/

textInputLatout.getEditText().addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() < 1) {
               textInputLayout.setErrorEnabled(true);
                textInputLayout.setError("Please enter a value");
            }

            if (s.length() > 0) {
                textInputLayout.setError(null);
                textInputLayout.setErrorEnabled(false);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

If you want to remove the error display from TextInputLayout use:-

YourtextInputLayout.setErrorEnabled(false);

else If you want to remove error display from edittextfield use:-

edittext_id.setError(null);

I suggest using Textwatcher fucntion in Android for more efficient validation handling..

eg:

editText.addTextChangedListener(new TextWatcher() { 
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!validatefName()) {
                    return;
                }
            }
        });

Use a if else condition,if condition is true set TextInoutLayoutObj.setError(null); if its false set your error there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!