How to delete instantly SPACE from an edittext if a user presses the space?

前端 未结 7 2052
星月不相逢
星月不相逢 2020-12-09 22:44

I have an edittext, and a textwatcher that watches if SPACE arrived or not. If its a SPACE I would like to delete that instantly. Or if its a space I want to make sure it do

7条回答
  •  佛祖请我去吃肉
    2020-12-09 23:01

    For removing the space instantly you can achieve it by two ways.

    One simple solution you can set the digits to your edit text.

    android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 
    

    second way you can set a filter

    EditText.setFilters(new InputFilter[] { filter });
    
    
    InputFilter filter = new InputFilter() {
     public CharSequence filter(CharSequence source, int start, int end,
       Spanned dest, int dstart, int dend) {
      for (int i = start; i < end; i++) {
       if (Character.isSpaceChar(source.charAt(i))) {
        return "";
       }
      }
      return null;
    
    
          }
         }
    

提交回复
热议问题