Android中EditText怎样禁用空格和汉字

使用InputFilter类来过滤空格,以前使用它过滤过汉字,unicode码

public static void inputFilterSpace(final EditText edit){
        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) {   
                                                       Log.d(TAG,"src:"+src+";start:"+start+";end:"+end);   
                                                       Log.d(TAG,"dest:"+dst+";dstart:"+dstart+";dend:"+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();
                                                       }
                                                      
                                                   }
                                               }          
                                             });    
    }

顺便也网上看了看,有人使用它过滤大小写字母,API:Character.isUpperCase(source.charAt(i))

附带:过滤unicode码:

判断utf-8码字:

//utf-8 字符判断
    public static boolean isUtf8String(String str) 
    {
        if(str == null)
        {
            return false;
        }
        byte[]  array= null;
        try {
            array= str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if((array != null)&&(array.length == str.length()))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

 

 

 

来源:http://blog.csdn.net/fuuckwtu/article/details/6968766

posted @ 2016-03-02 17:44  就这样e  阅读(2031)  评论(0编辑  收藏  举报