Android text editing box input character and length limit indicator control

Source: Internet
Author: User

1. Only letters, numbers, and underscores can be entered in the restricted editing box. Method 1: <EditText android: dights = "success _"/> problematic Method 2: InputFilter, but Character. isLetterOrDigit also regards Chinese as Letter, so this method cannot be limited to Chinese input. InputFilter filter = new InputFilter () {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 ))&&! Character. toString (source. charAt (I). equals ("_")&&! Character. toString (source. charAt (I )). equals ("-") {return "" ;}} return null ;}; 2. restrict the number of words in the edit box. Method 1: Use android in xml: method 2: Use LengthFilter to dynamically limit editText. setFilters (new InputFilter [] {new InputFilter. lengthFilter (MAX_LENGTH)}); 3. When using the word input limit display control, you only need to add the EditTextLengthIndicate control to the xml file and add editTextLengthIndicate to the code. bindEditText (editText, length) dynamically displays the red area information in the graph on the interface, indicating the user's input operations. The Code is as follows/*** inherited from TextView, used to display the number of words currently entered in the specified text box */public class EditTextLengthIndicate extends TextView {private EditText mEditText; private int mMaxLength; public void bindEditText (EditText et, int maxLength) {mEditText = et; mMaxLength = maxLength; setText (et. getText (). length () + "/" + mMaxLength); if (et! = Null & maxLength> 0) {et. addTextChangedListener (new TextWatcher () {@ Override public void onTextChanged (CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub String str = s. toString (); int length = str. length (); setText (length + "/" + mMaxLength );}});}}}

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.