Androidのinputfillter by character filter length, one Chinese when two characters

Source: Internet
Author: User

/*** 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

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.