Zookeeper
How can I set EditText so that only numbers or certain letters can be entered?
1. Set EditText and enter only numbers:
Method 1: directly generate the DigitsKeyListener object.
Et_1.setKeyListener (new
DigitsKeyListener (false, true ));
Method 2: Set attributes in EditText. android: numeric = "integer" indicates that only integers can be entered.
Android: singleLine = "true"
Android: numeric = "integer"
/>
Method 3: Create a char [] and add the characters that can be entered. As follows:
EditText. setKeyListener (new
NumberKeyListener (){
Protected char [] getAcceptedChars ()
{
Char []
NumberChars [] = {'1', '2', '3', '4', '5', '6', '7', '8 ', '9', '0 ',};
Return numberChars;
}
});
2. Set EditText to enter only some letters, such as the following settings edtitext can only enter A-N, a-n these letters. The method is as follows:
EditText. setKeyListener (new
NumberKeyListener (){
Protected char [] getAcceptedChars ()
{
Char []
NumberChars [] = {'a, 'B', 'C', 'D', 'E', 'F', 'A', 'B', 'C ', 'D '};
Return numberChars;
}
});
EditText et; et = (EditText) findViewById (R. id. et); // Method 1: Create a DigitsKeyListener and set it to your EditText KeyListenerDigitsKeyListener numericOnlyListener = new
DigitsKeyListener (false, true); et. setKeyListener (numericOnlyListener );//
Method 2: Set a NumberKeyListener for EditText, And Then override the getAcceptedChars () method and the getInputType () method et. setKeyListener (new NumberKeyListener () {@ Overrideprotected char [] getAcceptedChars () {return new char [] {'1', '2', '3', '4 ', '5', '6', '7', '8', '9', '0'
};}@ Overridepublic int getInputType () {// TODO Auto-generated method stubreturn android. text. InputType. TYPE_CLASS_PHONE ;}});
Bytes --------------------------------------------------------------------------------------------
01. editText et; 02.et = (EditText) findViewById (R. id. et); 03. // Method 1: Create a DigitsKeyListener and set it to KeyListener04.DigitsKeyListener numericOnlyListener = new of EditText.
DigitsKeyListener (false, true); 05. et. setKeyListener (numericOnlyListener); 06 .//
Method 2: Set a NumberKeyListener for EditText, And Then override the getAcceptedChars () method and getInputType () method 07. et. setKeyListener (new NumberKeyListener () {08. @ Override09. protected char [] getAcceptedChars () {10. return new char [] {'1', '2', '3', '4', '5', '6', '7', '8 ', '9 ',
'0'}; 11 .} 12. @ Override13. public int getInputType () {14. // TODO Auto-generated method stub15. return android. text. inputType. TYPE_CLASS_PHONE; 16 .} 17 .});
Summary:
First, you can enter decimal places.
The second type can only be an integer because TYPE_CLASS_PHONE is set. And flexible.
========================================================== ====
Many users may find that EditText sometimes needs to restrict the content entered by users when developing Android. Generally, we can use regular expressions to directly limit the content, but Android
The EditText input type has been prepared for us, which has the following advantages over regular expressions:
1. Simpler development and efficient execution. 2.
The input method is changed by default. For example, if it is set to numeric, the input method automatically displays only numbers without any letters in Qwerty.
Next we use layout of EditText
To implement the following functions:
1. password box attribute android: password = "true"
The content displayed in EditText is automatically set to asterisk (*). The entered content is changed to * within 1 second.
2. Pure Digital android: numeric = "true"
This option automatically changes the input method to a numeric Input Keyboard, and only 0-9 numbers are allowed.
3. Only android: capitalize = "cwj1987" is allowed"
In this way, only cwj1987 is allowed for password verification.
Below are some extended style attributes
Android: editable = "false"
EditText cannot be edited
Android: singleLine = "true"
The mandatory input content is in a single line.
Android: ellipsize = "end"
The tail overflow data is automatically hidden, which is generally used when the text content is too long to be displayed in all rows.