Maximum value input in Android EditText

Source: Internet
Author: User

Maximum value input in Android EditText

Sometimes, we add the maximum length limit to EditText. When EditText reaches the input limit, it is blocked from entering the text and a prompt is displayed.

This problem seems simple, but it is difficult to handle perfectly.

Because EditText cannot intercept software disk events, problems may occur in general methods such as TextWatcher and OnKeyListener. After reverse review and error, we finally found the following solutions:


1. Implement an InputFilter, judge the input length in InputFilter, and a prompt is displayed:

   public class LengthFilter implements InputFilter {        public LengthFilter(int max) {            mMax = max;        }        @Override        public CharSequence filter(CharSequence source, int start, int end,                Spanned dest, int dstart, int dend) {            int keep = mMax - (dest.length() - (dend - dstart));            if (keep <= 0) {                if (mErrorToast == null) {                    mErrorToast = Toast.makeText(ShowDetailCommentActivity.this, R.string.comment_input_overflow,                            Toast.LENGTH_SHORT);                }                mErrorToast.show();                return "";            } else if (keep >= end - start) {                return null; // keep original            } else {                keep += start;                if (mErrorToast == null) {                    mErrorToast = Toast.makeText(ShowDetailCommentActivity.this, R.string.comment_input_overflow,                            Toast.LENGTH_SHORT);                }                mErrorToast.show();                if (Character.isHighSurrogate(source.charAt(keep - 1))) {                    --keep;                    if (keep == start) {                        return "";                    }                }                return source.subSequence(start, keep);            }        }        private int mMax;    }

2. Set this InputFilter to the corresponding EditText:

mEditText.setFilters(new InputFilter[] { new LengthFilter(mMaxTextNum) });


The code is small but valid. You can try it.



Author: xzy2046, which must be noted for reprinting. Blog homepage: http://blog.csdn.net/xzy2046



Related Article

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.