How to restrict TextView to allow only alpha-numeric characters in Android

后端 未结 4 1055
一生所求
一生所求 2020-12-09 04:13

I have a TextView in my app that i want a user to be able to only enter alpha-numeric characters in. How can this be done? Thanks!

4条回答
  •  情歌与酒
    2020-12-09 04:56

    Here is a better solution......... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA

    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))) { 
                                    return ""; 
                            } 
                    } 
                    return null; 
            } 
    }; 
    
    edit.setFilters(new InputFilter[]{filter});
    

提交回复
热议问题