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

前端 未结 7 2008
星月不相逢
星月不相逢 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;
    
    
          }
         }
    
    0 讨论(0)
  • 2020-12-09 23:03

    My relatively simple solution for instant whitespace deletion without removing spannables (styles) in EditText:

    1. Remove at start:

          @Override
      public void afterTextChanged(Editable s) {
          int i;
          for (i = 0; i < s.length() && Character.isWhitespace(s.charAt(i)); i++) { ; }
          s.replace(0, i, "");
      }
      

    Basically that's it, but you can also do:

    1. Remove at start (without interrupting first input):

          @Override
      public void afterTextChanged(Editable s) {
          String text = s.toString();
          if(!text.trim().isEmpty()){
              int i;
              for (i = 0; i < s.length() && Character.isWhitespace(s.charAt(i)); i++) { ; }
              s.replace(0, i, "");
          }
      }
      

    1. Removing at start and end (allow 1 whitespace at end for convinient input):

          @Override
      public void afterTextChanged(Editable s) {
          int i;
          //remove at start
          for (i = 0; i < s.length() && Character.isWhitespace(s.charAt(i)); i++) { ; }
          s.replace(0, i, "");
          //remove at end, but allow one whitespace character
          for (i = s.length(); i > 1 && Character.isWhitespace(s.charAt(i-1)) && Character.isWhitespace(s.charAt(i-2)); i--) { ; }
          s.replace(i, s.length(), "");
      }
      
    0 讨论(0)
  • 2020-12-09 23:11
    @Override
    public void afterTextChanged(Editable s) {
        String result = s.toString().replaceAll("\\s", "");
        if (!s.toString().equals(result)) {
            int pos = editText.getSelectionStart() - (s.length() - result.length());
            editText.setText(result);
            editText.setSelection(Math.max(0,Math.min(pos, result.length())));
            editText.setError("No spaces allowed");
        }
    }
    

    \s matches any whitespace character (equal to [\r\n\t\f\v ])

    Setting selection like this, allow you to enter or paste text in middle of edittext without loosing cursor position

    0 讨论(0)
  • 2020-12-09 23:12

    setSelection is there to set the cursor again at the end of your EditText:

    editText.addTextChangedListener(new TextWatcher() {
    
                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2,
                            int arg3) {}
                    @Override
                    public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                            int arg3) {}
                    @Override
                    public void afterTextChanged(Editable arg0) {
                        if(editText.getText().toString().contains(" ")){ editText.setText(editText.getText().toString().replaceAll(" " , ""));
                        editText.setSelection(editText.getText().length());
    
                        Toast.makeText(getApplicationContext(), "No Spaces Allowed", Toast.LENGTH_LONG).show();
                        }
                    }});
    
    0 讨论(0)
  • 2020-12-09 23:13

    One more simple way to achieve this using the input Filter

    editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.toString().equalsIgnoreCase(" ")){
                    return "";
                }
                return source;
            }
        }});
    

    This will remove the space entered by the user immediately and gives appearance like space is disabled.

    0 讨论(0)
  • 2020-12-09 23:21

    The solution is as usually much simpler:

    @Override
    public void afterTextChanged(Editable s) {
        String result = s.toString().replaceAll(" ", "");
        if (!s.toString().equals(result)) {
             ed.setText(result);
             ed.setSelection(result.length());
             // alert the user
        }
    }
    

    This shouldn't have the problems of the previous attempts.

    0 讨论(0)
提交回复
热议问题