Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

How to exclude special characters from android keypad for EditText

Hi I want to show only numbers and characters on the keypad for EditText in android, I did try to add the attribute android:inputType = text|number but it did not work.

Please help me with any other better suggestion. thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the filter for that. Here I am adding the code for filter.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...