android edittext inputfilter should accept space,c

2019-05-29 04:44发布

问题:

street = (EditText) findViewById(R.id.street);
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.isLetterOrDigit(source.charAt(i)) || !Character.isSpaceChar(source.charAt(i))) { 
                 return "";     
             }     
        }
        return null;   
    }  
};
street.setFilters(new InputFilter[] { filter });

my edittext is able to filter character & number on the virtual keyboard but not taking the space character.. plz help

回答1:

instead of '||' i replaced with '&&' and got the answer....



回答2:

!Character.isLetterOrDigit(source.charAt(i)) || Character.isSpaceChar(source.charAt(i))

Is the code you want assuming you DONT want white space characters and only numbers or characters.



回答3:

Use this condition

for (int i = start; i < end; i++) { 
    if (!Character.isLetterOrDigit(source.charAt(i))) {
        if (!Character.isSpaceChar(source.charAt(i)))
            return "";
    }
}