EditView input limit (soft keyboard limit)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.