Show Error on the tip of the Edit Text Android

前端 未结 12 2102
一生所求
一生所求 2020-12-01 04:30

I want to show error if the user enters blank value in the edittext.But i am not getting the way how could i do this .This is how i want like this:

12条回答
  •  渐次进展
    2020-12-01 05:03

    You have written your code in onClick event. This will call when you click on EditText. But this is something like you are checking it before entering.

    So what my suggestion is, you should use focus changed. When any view get focus, you are setting no error and when focus changed, you check whether there is valid input or not like below.

    firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            firstName.setError(null);
            if (firstName.getText().toString().trim().equalsIgnoreCase("")) {
                firstName.setError("Enter FirstName");
            }
        }
    });
    

提交回复
热议问题