简体   繁体   中英

How to check if EditText still has error message on clicking submit button

I am creating a Sign-Up form.

I have used setError() to set error messages in all EditText s. now I want to check that whether an EditText has error message or not when I click on the submit button.

To check text after it has been written but before clicking register/submit button on your page, you need to use TextWatcher

After setting up error with setError() , you need to check if getError() returns null as well as editText.getText().length() returns 0 then perform action like Toast or anything.

Eg:

onClick(View v){
    if(editText.getError() != null || editText.getText().length() == 0){
        ................(Perform Toast or something for clearing error)
    }else{
        .... your action to perform on button click
    }

You could use the getError() method for the EditText and check if it returns null . Although a better way would be to just set a boolean isValid on errors and check against that

Right approach to achieve an error less submission of your form will be following:

  1. on click of submit check whether entered text is a correct email.
  2. if its not correct show error using setError .
  3. if its correct go ahead and submit your form.

you need not to check explicitly if error is set or not as upon next submit field will be re-verified anyways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM