SoFunction
Updated on 2025-04-10

Android programming method to implement partial color changes in TextView

Android programming method to implement partial color changes in TextView

Updated: December 12, 2015 10:29:28 Author: lee0oo0
This article mainly introduces the method of Android programming to implement partial color changes in TextView. The example analyzes the relevant skills of setting the background color and font color of the TextView at the specified position. Friends who need it can refer to it.

This article describes the method of Android programming to implement partial color changes in TextView. Share it for your reference, as follows:

public class StringHandleExampleActivity extends Activity {
  /** Called when the activity is first created. */
  private TextView textView;
  private String tempStr = "abcd12my medieval zx9yu5!f3,,";
  private StringBuffer sb;
  private List<Integer> list;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    sb = new StringBuffer();
    list = new ArrayList<Integer>();
    textView = (TextView) findViewById();
    for (int i = 0; i < (); i++) {
      if (matcherReg(((i)))) {
        (i);
      }
    }
    SpannableStringBuilder style=new SpannableStringBuilder(tempStr);
    for (int i = 0; i < (); i++) {
      ((i)+", ");
// (new BackgroundColorSpan(),(i),(i)+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //Set the background color of the specified location textview      (new ForegroundColorSpan(),(i),(i)+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   //Set the color of the text at the specified location    }
    (style); 
  }
  private boolean matcherReg(CharSequence c){
    String regEx="[^0-9]";  
    Pattern p = (regEx);  
    Matcher m = (());  
    if (()) {
      return false;
    }
    return true;
  }
}

I hope this article will be helpful to everyone's Android programming design.

  • Android
  • TextView
  • color

Related Articles

  • Use findViewByMe in Android to improve component search efficiency

    This article mainly introduces the relevant knowledge of using findViewByMe in Android to improve component search efficiency. It has good reference value. Let's take a look with the editor below
    2017-03-03
  • A summary of common attribute functions of Android EditText

    This article mainly introduces the common attribute functions of Android EditText, and summarizes and analyzes the functions and specific usages of the common attributes of the EditText control in Android development. Friends who need it can refer to it.
    2016-06-06
  • Add TextView strikethrough on Android (simple code)

    I recently took on a project, and there are project requirements like this. There are such demands, that is, there are two prices under a product, one is the market price and the other is the sales price. At this time, a delete line must be added to the market price; how to achieve it? Below, the editor will share with you a simple code to implement Android to add TextView delete line
    2016-02-02
  • Detailed explanation of Android image performance optimization

    This article shares relevant knowledge points about Android performance optimization and picture optimization. Interested friends will refer to it.
    2018-07-07
  • Android practical tutorial 43: Top loading and drop-down refresh

    This article mainly introduces the 43rd Android practical tutorial, the upper loading and pull-down refresh, which has certain reference value. Interested friends can refer to it.
    2016-11-11
  • Android string to Ascii code instance code

    This article mainly introduces the method of converting Android strings to Ascii code. Please refer to it.
    2013-11-11
  • Simple way to determine the orientation of the camera picture

    Below, the editor will bring you an easy way to determine the orientation of the camera image on Android. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor
    2017-03-03
  • Android Coil vs. Glide in-depth analysis and exploration

    This article mainly introduces Android Coil vs. Glide. Coil is a brand new image loading framework on Android. Its full name is coroutine image loader, that is, coroutine image loading library
    2023-02-02
  • ObjectAnimator attribute animation source code analysis

    Today, the editor will share with you an analysis of the source code of ObjectAnimator attribute animation. The editor thinks the content is quite good. Now I share it with you. It has good reference value. Friends in need will follow the editor to take a look.
    2019-01-01
  • Android WebView FAQs and solutions

    This article mainly introduces the common problems and solutions of Android WebView. Friends who need it can refer to it.
    2015-08-08

Latest Comments