📜  如何使用SpannableString类在android中更改子字符串的文本颜色?

📅  最后修改于: 2021-05-10 17:38:57             🧑  作者: Mango

在本文中,我们将学习如何更改字符串的子字符串的文本颜色。更改整个字符串的颜色很容易,但是要更改子字符串的颜色,我们必须使用特殊的类SpannableString 。但是,当要更改文本的背景颜色时,SpannableString类并不是真正有用。因此,我们必须使用SpannableStringBuilder类。

方法:

  1. 在activity_main.xml文件中添加以下代码。这将在activity_main布局中添加两个textview
    activity_main.xml
    
    
      
        
        
    


    MainActivity.java
    package org.geeksforgeeks.gfgspannablestring;
      
    import android.graphics.Color;
    import android.os.Bundle;
    import android.text.SpannableString;
    import android.text.SpannableStringBuilder;
    import android.text.Spanned;
    import android.text.style.BackgroundColorSpan;
    import android.text.style.ForegroundColorSpan;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            TextView textView = findViewById(R.id.text_view);
            TextView textView2 = findViewById(R.id.text_view2);
      
            String text = "GeeksForGeeks A Computer 
                           Science Portal for Geeks";
            String text2 = "Learn Algorithm.";
      
            SpannableString spannableString = new SpannableString(text);
      
            // we can only use backgroundcolor
            // span with a spannableStringBuilder.
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text2);
      
            // It is used to set foreground color.
            ForegroundColorSpan green = new ForegroundColorSpan(Color.GREEN);
            ForegroundColorSpan cyan = new ForegroundColorSpan(Color.CYAN);
      
            // It is used to set background color.
            BackgroundColorSpan yellow = new BackgroundColorSpan(Color.YELLOW);
      
            // It is used to set the span to the string
            spannableString.setSpan(green,
                                    0, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(cyan,
                                    40, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      
            spannableStringBuilder.setSpan(yellow,
                                           0, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      
            textView.setText(spannableString);
            textView2.setText(spannableStringBuilder);
        }
    }


  2. 现在,在MainActivity中添加以下代码。 Java文件。在这段代码中,我们将改变第一的TextView的子串的颜色与SpannableString类,并与SpannableStringBuilder第二TextView中添加背景颜色。使用要显示的文本创建类的对象,并使用setSpan函数更改子字符串的颜色。

    主要活动。Java

    package org.geeksforgeeks.gfgspannablestring;
      
    import android.graphics.Color;
    import android.os.Bundle;
    import android.text.SpannableString;
    import android.text.SpannableStringBuilder;
    import android.text.Spanned;
    import android.text.style.BackgroundColorSpan;
    import android.text.style.ForegroundColorSpan;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            TextView textView = findViewById(R.id.text_view);
            TextView textView2 = findViewById(R.id.text_view2);
      
            String text = "GeeksForGeeks A Computer 
                           Science Portal for Geeks";
            String text2 = "Learn Algorithm.";
      
            SpannableString spannableString = new SpannableString(text);
      
            // we can only use backgroundcolor
            // span with a spannableStringBuilder.
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text2);
      
            // It is used to set foreground color.
            ForegroundColorSpan green = new ForegroundColorSpan(Color.GREEN);
            ForegroundColorSpan cyan = new ForegroundColorSpan(Color.CYAN);
      
            // It is used to set background color.
            BackgroundColorSpan yellow = new BackgroundColorSpan(Color.YELLOW);
      
            // It is used to set the span to the string
            spannableString.setSpan(green,
                                    0, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(cyan,
                                    40, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      
            spannableStringBuilder.setSpan(yellow,
                                           0, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      
            textView.setText(spannableString);
            textView2.setText(spannableStringBuilder);
        }
    }
    

输出: