/*** Lengthfilter implemented in byte number mode *@authorBvin*/ Public classOnebyteinputfilterImplementsinputfilter{Private Final intMmax; PublicOnebyteinputfilter (intMmax) { Super(); This. Mmax =Mmax; } @Override PublicCharsequence Filter (charsequence source,intStartintEnd, spanned dest,intDStart,intdend) { //int remainlength = Dest.length ()-(Dend-dstart);charsequence Placestart= Dest.subsequence (0, DStart); Charsequence Placeend=dest.subsequence (DEnd, Dest.length ()); intPlacestartlength =Util.computelenthbybyte (placestart.tostring ()); intPlaceendlength =Util.computelenthbybyte (placeend.tostring ()); intCanreplacesize = mmax-placestartlength-placeendlength; if(canreplacesize>0) {charsequence Sourcespan=source.subsequence (start, end); intSourcespanlength =Util.computelenthbybyte (sourcespan.tostring ()); if(sourcespanlength>=canreplacesize) {//replacement content is longer than the length of the FitString result = Fixsplit (Sourcespan.tostring (), Canreplacesize,false); if(!textutils.isempty (Result)) { returnresult; }Else { return""; } }Else { return NULL; } }Else { return""; } } Private StaticString Fixsplit (String s,intSumBooleanDebug) { intCounter = 0; String fixstring=NULL; for(inti = 0; I < s.length (); i++) { if(counter>=sum) { Break; } intLength = (S.charat (i) + ""). GetBytes (). length; if(length>1) {counter+ = 2; if(counter<=sum) {fixstring= s.substring (0, i+1); } } Else if(length==1){ if(counter<=sum) {fixstring= s.substring (0, i+1); } ++counter; }Else{//less than 1 Continue; } /*if (counter>=sum) {System.out.println ("break;" +counter); Break }else {fixstring = s.substring (0, i+1); }*/ } returnfixstring; }}
Androidのinputfillter by character filter length, one Chinese when two characters