The method of implementing TextView partial color change by Android programming _android

Source: Internet
Author: User
Tags stringbuffer

The example in this article describes the way that Android programming implements some of the color changes in TextView. Share to everyone for your reference, specific as follows:

The public class Stringhandleexampleactivity extends activity {/** called the ' when the ' is ' the ' activity ' is the ' the '. * Private
  TextView TextView;
  Private String TempStr = "Abcd12 My medieval Zx9yu5!f3,";
  Private StringBuffer SB;
  Private list<integer> List;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    SB = new StringBuffer ();
    List = new arraylist<integer> ();
    TextView = (TextView) Findviewbyid (R.id.textview); for (int i = 0; i < tempstr.length (); i++) {if (Matcherreg (string.valueof (Tempstr.charat (i))) {List.ad
      D (i);
    } spannablestringbuilder style=new Spannablestringbuilder (TEMPSTR);
for (int i = 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_inclusive   ); Sets the background color of the TextView at the specified location Style.setspan (new FOregroundcolorspan (color.red), List.get (i), List.get (i) +1,spannable.span_exclusive_inclusive); 
  Sets the color of the specified location text} textview.settext (style);  
    Private Boolean Matcherreg (Charsequence c) {String regex= ' [^0-9] ';  
    Pattern p = pattern.compile (regEx);  
    Matcher m = P.matcher (C.tostring ());
    if (M.matches ()) {return false;
  return true;

 }
}

I hope this article will help you with your Android programming.

Related Article

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.