How to detect special characters in an edit text and display a Toast in response (Android)?

前端 未结 6 1837
既然无缘
既然无缘 2021-01-01 01:06

An apology for my bad English, I\'m using google translate.

I\'m creating an activity in which users must create a new profile. I put a limit to edit text of 15 char

6条回答
  •  死守一世寂寞
    2021-01-01 01:30

    As I can´t comment on other posts, I will complement the most voted answer with a tip for a more fashion visual resolution.

    First, pass the Edittext (etNombre) data to a String variable. Ex: String nombre = etNombre.getText().toString();

    Then, use an if to verify:

    if (!nombre.matches("[a-zA-Z.? ]*")) {
    
        etNombre.setError("Your message here");
    
    }
    

    this solution will set an "!" icon on the Edittext and it's much better than a Toast because the message is pointing the user to the error.

提交回复
热议问题