开发者

android edittext inputfilter should accept space,character & number

开发者 https://www.devze.com 2023-01-31 04:54 出处:网络
street = (EditText) findViewById(R.id.street); InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, in开发者_JAVA技巧t end,Spanned dest, int dstart, int
street = (EditText) findViewById(R.id.street);
InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, in开发者_JAVA技巧t 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


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


!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.


Use this condition

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号