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

前端 未结 6 1836
既然无缘
既然无缘 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:50

    use the the following line in edittext in xml file so that it only enters the alphabets and numbers;

     android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    

    If you need dynamic response use textwatcher for your edittext;

       yourEditText.addTextChangedListener(new TextWatcher() {
    
          public void afterTextChanged(Editable s) {
    
           // Here you need to check special character, if found then show error message
    
          if (s.toString().contains("%"))
          {
               // Display error message
          }
    
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });
    

提交回复
热议问题