Android 使用setFilter 过滤空格

项目要求注册用户时屏蔽空格,使用InputFilter类来过滤空格

<span style="white-space:pre">					</span>edit.setFilters(new InputFilter[]
                                           {
                                            new InputFilter.LengthFilter(16),
                                               new InputFilter(){    
                                                   public CharSequence filter(CharSequence src, int start, int end, Spanned dst, int dstart, int dend) {   
                                                       
                                                       if(src.length()<1)
                                                       {
                                                            return null;
                                                       }
                                                       else
                                                       {
                                                           char temp [] = (src.toString()).toCharArray();
                                                           char result [] = new char[temp.length];
                                                           for(int i = 0,j=0; i< temp.length; i++){
                                                               if(temp[i] == ' '){
                                                                   continue;
                                                               }else{
                                                                   result[j++] = temp[i];
                                                               }
                                                           }
                                                           return String.valueOf(result).trim();
                                                       }
                                                      
                                                   }
                                               }          
                                             });    


你可能感兴趣的:(java,android,filter)