How do I use InputFilter to limit characters in an EditText in Android?

后端 未结 20 1477
慢半拍i
慢半拍i 2020-11-22 04:23

I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to digits but I cannot figure out the ways of Inputfilter looking through the

20条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 05:00

    to prevent words in edittext. create a class that u could use anytime.

    public class Wordfilter implements InputFilter
    {
        @Override
        public CharSequence filter(CharSequence source, int start, int end,Spanned dest, int dstart, int dend) {
            // TODO Auto-generated method stub
            boolean append = false;
            String text = source.toString().substring(start, end);
            StringBuilder str = new StringBuilder(dest.toString());
            if(dstart == str.length())
            {
                append = true;
                str.append(text);
            }
            else
                str.replace(dstart, dend, text);
            if(str.toString().contains("aaaaaaaaaaaa/*the word here*/aaaaaaaa"))
            {
                if(append==true)
                    return "";
                else
                    return dest.subSequence(dstart, dend);
            }
            return null;
        }
    }
    

提交回复
热议问题