Android TextView part color change

Source: Internet
Author: User

 Public classStringhandleexampleactivityextendsActivity {/**Called when the activity is first created.*/        PrivateTextView TextView; PrivateString TempStr = "Abcd12 My medieval Zx9yu5!f3,"; PrivateStringBuffer SB; PrivateList<integer>list; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.main); SB=NewStringBuffer (); List=NewArraylist<integer>(); TextView=(TextView) Findviewbyid (R.id.textview);  for(inti = 0; I < tempstr.length (); i++) {            if(Matcherreg (string.valueof (Tempstr.charat (i))) {List.add (i); }} Spannablestringbuilder Style=NewSpannablestringbuilder (TEMPSTR);  for(inti = 0; I < list.size (); i++) {System.out.println (List.get (i)+", ");//Style.setspan (New Backgroundcolorspan (color.red), List.get (i), List.get (i) +1,spannable.span_exclusive_inclus     IVE); //sets the background color of the TextView at the specified positionStyle.setspan (NewForegroundcolorspan (color.red), List.get (i), List.get (i) +1,spannable.span_exclusive_inclusive);//sets the color of text at the specified position} textview.settext (style); }        Private BooleanMatcherreg (charsequence c) {String regEx= "[^0-9]"; Pattern P=Pattern.compile (regEx); Matcher m=P.matcher (c.tostring ()); if(M.matches ()) {return false; }        return true; }    }

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.