Android editText max limit

after I set the maximum character limit for editText using InputFilter.LengthFilter, is there a way to get editText to respond to onTextChanged after reaching the maximum number of characters? if not, is there an easy way to set the maximum number of characters in editText and still respond to the onTextChanged event? Thanks

+5
source share
2 answers

In the XML itself for EditText, specify the following attribute:

  android:maxLength="20"

Thank.

+12
source

, SO . @njzk2 . , .

maxLength EditText InputFilter, .

InputFilter.LengthFilter, null, . (. )

: InputFilter.LengthFilter, super null, , .

editText.setInputFilters(new InputFilter[] {
    new InputFilter.LengthFilter(max) {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            CharSequence res = super.filter(source, start, end, dest, dstart, dend);
            if (res != null) { // Overflow
                   Toast.makeText(this,"OVERFLOW!",Toast.LENGTH_SHORT).show();

            }
            return res;
        }
    }
});
0

All Articles