How to restrict Android editing text box to only some selected characters

Is there a way to restrict users who are allowed to enter only some selected character set (for example: A or B or C or D) in Android layout.xml

+7
source share
4 answers

Use the digits attribute, only characters passed to digits will be allowed in EditText.

 android:digits="abc123" 

And yes, β€œnumbers” is a misleading name; it also accepts letters and symbols.

+12
source

You can try the following:

  <EditText android:id="@+id/edit_text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:iputType="numbers" /> 

The possible values ​​for the android:inputType attribute android:inputType in the editing text are: none, text, textCapCharacters, textCapWords, textCapSentences, textAutoCorrect, textAutoComplete, textMultiLine, textImeMultiLine, textNoSuggestions, textUri, textEmailAddress, textEmailSubject, textShortMessage, textLongMessage, textPersonName, textPostalAddress, textPassword, textVisiblePassword, textWebEditText, textFilter, textPhonetic, textWebEmailAddress, textWebPassword, number, numberSigned, numberDecimal, numberPassword, phone, datetime, date, time

+2
source

This android:digit will only contain the numbers and alphabets that you want to fill in with EditText.

 <EditText android:inputType="text" android:digits="0123456789*qwertzuiopasdfghjklyxcvbnm,_,-" android:hint="Only letters, digits, _ and - allowed" /> 
0
source

You can set the EditText field as follows:

 <EditText android:inputType="text" android:digits="THE SELECTED CHARACTERS THAT USERS ARE ALLOWED TO TYPE" /> 

By listing the selected characters (A, B, C, and D) so that users are allowed to enter numbers android: numbers, you limit the possible characters that users can enter. This is the easiest way I found out when searching.

0
source

All Articles