Public classLengthfilterImplementsInputfilter { PublicLengthfilter (intmax) {Mmax=Max; } @Override PublicCharsequence Filter (charsequence source,intStartintend, spanned dest,intDStart,intdend) { intKeep = 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""; } } returnsource.subsequence (start, keep); } } Private intMmax; }
Medittext.setfilters (newnew lengthfilter (Mmaxtextnum)});
The code is very small but effective, you can try it.
xzy2046, reprint need to indicate. Blog home: http://blog.csdn.net/xzy2046
Android edittext Input Max-value prompt function