It is well known that EditView has a inputtype property to set the type of input.
You can only enter numbers if you set the following:
Android:inputtype= "Number"
However, sometimes you need to customize the input constraints, such as the first bit can only be "1", altogether 11 bits, more than 11 bits can not be entered, or only allow to enter less than 5 of the number, etc., you need additional settings. There are three ways to set up in Android. The first type:digitsThe property is set as follows: only numbers between 0-5 and up to 11 bits can be entered.
1 <EditText2 android:id= "@+id/etdigits"3 android:layout_width= " Match_parent "4 android:layout_height=" Wrap_content "5 android:digits=" 012345 "6 android:hint= "set by digits"7 android:inputtype= "number"8 android: Maxlength= "One"/>
PS: Advantages: simple and convenient;Cons: You can only set a simple limit, if you need to set the more complex, such as the number of letters and some specific characters need to be digits in the poor. Second type: Textwatcher settingsThis way is also more flexible, a good way.
1EditText Ettextwatcher =(EditText) Findviewbyid (r.id.ettextwatcher);2Ettextwatcher.addtextchangedlistener (NewEdittextwatcher (Ettextwatcher));3 /**4 * Created by Xugang on 2016/6/23.5 * Input Monitoring6 */7 Public classEdittextwatcherImplementsTextwatcher {8 PrivateEditText EditText;9 PublicEdittextwatcher (EditText EditText) {Ten This. EditText =EditText; One } A @Override - Public voidBeforetextchanged (Charsequence S,intStartintCountintAfter ) { - } the @Override - Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) { - if(s! =NULL&& s.length () >start) { - //Enter judgment rules + if(!Regularutils.compare (s.tostring (). Trim (), regularutils.limit_phone_input)) { -Edittext.settext (s.subsequence (0, S.length ()-1)); +Edittext.setselection (S.length ()-1); A } at } - } - @Override - Public voidaftertextchanged (Editable s) { - } -}
The third type: SetFilters, it is possible to restrict incoming regular rules by regular means, which is more flexible.
1 Public Static FinalString limit_phone_input = "^1\\d{0,10}$";//limit the number of up to 11 digits that are only allowed to enter the first 12EditView Etinputfilter =(EditView) Findviewbyid (r.id.etinputfilter);3Etinputfilter.setfilters (NewEditinputfilter (Limit_phone_input));4 /**5 * Created by Xugang on 2016/6/22.6 * EditView Filter7 */8 Public classEditinputfilterImplementsInputfilter {9 //Ten PrivateString Regular; One PublicEditinputfilter (String Regular) { A This. Regular =Regular; - } - @Override the PublicCharsequence Filter (charsequence source,intStartintEnd, spanned dest,intDStart,intdend) { - if(Regular = =NULL) - returnSource; - if(DStart = = 0 && DEnd = = 0 &&textutils.isempty (source)) + return NULL; - if(DStart = =dend) { + //input AStringBuilder Builder =NewStringBuilder (dest); at if(Builder.append (source). ToString (). Matches (regular))returnSource; - Else return""; - } - returnSource; - } -}
EditView input limit (soft keyboard limit)