Android use text watcher to prevent typing of special characters [duplicate]

廉价感情. 提交于 2019-12-06 03:17:23

问题


I want to make an EditText box and use TextWatcher to prevent typing of special characters.


回答1:


This is for only character and space validation...

InputFilter filter = new InputFilter()     
{  
    @Override  
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)   
    {   
        for (int i = start; i < end; i++)
            if (!Character.isLetter(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i)))
                return "";

        return null;  
    }
};

myTextView.setFilters(new InputFilter[] { filter });


来源:https://stackoverflow.com/questions/11179158/android-use-text-watcher-to-prevent-typing-of-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!