首页 > 代码库 > EditText禁止输空格

EditText禁止输空格

1.EditText禁止输空格

editText.setFilters(new InputFilter[]{filter});

private InputFilter filter=new InputFilter() {
   @Override
   public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//返回null表示接收输入的字符,返回空字符串表示不接受输入的字符
if(source.equals(" "))return ""; else return null; }};
2.EditText禁止输空格+换行键

editText.setFilters(new InputFilter[]{filter});
private InputFilter filter=new InputFilter() {
   @Override
   public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
      if(source.equals(" ")||source.toString().contentEquals("\n"))return "";
      else return null;
   }
};


EditText禁止输空格