SpannableString and SpannableStringBuilder are classes for setting text styles.

SpannableString and SpannableStringBuilder can achieve the effect

The name of the class Implementation effect
BackgroundColorSpan The background color
ClickableSpan Text is clickable, there are click events
ForegroundColorSpan Foreground color, text color
MaskFilterSpan Embellishes such as BlurMaskFilter, EmbossMaskFilter
RasterizerSpan Effect of grating
StrikethroughSpan Delete the line (underlined)
SuggestionSpan It’s a placeholder
UnderlineSpan The underline
AbsoluteSizeSpan Absolute size (text font
DynamicDrawableSpan Set images based on text baseline or bottom alignment
ImageSpan The picture
RelativeSizeSpan Relative size (text font
ScaleXSpan Scale on the x axis
StyleSpan Font style: bold, italic, etc
SubscriptSpan Subscripts (the mathematical formula will be used
SuperscriptSpan Superscript (the mathematical formula will be used
TextAppearanceSpan Text appearance (including font, size, style, and color)
TypefaceSpan The text font
URLSpan Text hyperlink

SpannableString and SpannableStringBuilder usage scenarios

The name of the class The variable text The variable tag The data structure
SpannableString no is Linear array
SpannableStringBuilder is is Interval tree
Here’s how to decide which class to use:
  • You need to attach a small number of spans to a single text object, and the text itself is read-only, using SpannableString
  • You need to modify the text after it is created, and you need to attach the Span to the text, using the SpannableStringBuilder
  • You need a lot of Span objects, and you have SpannableStringBuilder regardless of whether the text itself is read-only

Style implementation, setSpan

The text style is set by setSpan(Object What, int Start, int End, int flags). The what argument refers to the Span to be referenced to the text, and the start and end arguments refer to the text index to which the Span is applied. The flags argument is used to determine whether the Span contains the text of the start and end index objects. The meanings of flags values are shown in the following table:

The value of the flags Implementation effect
Spannable.SPAN_INCLUSIVE_EXCLUSIVE Insert new text before the text, use the style, insert new text after the style does not use the style
Spannable.SPAN_INCLUSIVE_INCLUSIVE This style is used when new text is inserted before and after the text
Spannable.SPSPAN_EXCLUSIVE_EXCLUSIVE This style is not used when new text is inserted before and after the text
Spannable.SPAN_EXCLUSIVE_INCLUSIVE Insert new text before the text without using the style, and insert new text after using the style
Note: If no text is inserted, the effect of the 4 flags is the same for changing the text color. They are all front closed and back open, including the front index and not the back index.
Display the effect of 4 flags values:
The code is as follows:
String text = "Time flies, time flies.";
tv_content.setText(text);

String text1 = "Time flies, time flies.";
SpannableStringBuilder spannableString1 = new SpannableStringBuilder(text1);
spannableString1.setSpan(new ForegroundColorSpan(Color.RED),2.7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString1.insert(2."aa");
spannableString1.insert(5."aa");
spannableString1.insert(11."aa");
tv_content1.setText(spannableString1);

String text2 = "Time flies, time flies.";
SpannableStringBuilder spannableString2 = new SpannableStringBuilder(text2);
spannableString2.setSpan(new ForegroundColorSpan(Color.BLUE),2.7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
spannableString2.insert(2."bb");
spannableString2.insert(5."bb");
spannableString2.insert(11."bb");
tv_content2.setText(spannableString2);

String text3 = "Time flies, time flies.";
SpannableStringBuilder spannableString3 = new SpannableStringBuilder(text3);
spannableString3.setSpan(new ForegroundColorSpan(Color.GREEN),2.7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString3.insert(2."cc");
spannableString3.insert(5."cc");
spannableString3.insert(11."cc");
tv_content3.setText(spannableString3);

String text4 = "Time flies, time flies.";
SpannableStringBuilder spannableString4 = new SpannableStringBuilder(text4);
spannableString4.setSpan(new ForegroundColorSpan(Color.YELLOW),2.7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString4.insert(2."dd");
spannableString4.insert(5."dd");
spannableString4.insert(11."dd");
tv_content4.setText(spannableString4);
Copy the code

SpannableStringBuilder example use

The effect is as follows:

The code is as follows:

String text5 = "My phone number is 123456789; My email is [email protected]; Head:";
SpannableStringBuilder spannableString5 = new SpannableStringBuilder(text5);
// Set the background color
spannableString5.setSpan(new BackgroundColorSpan(Color.GRAY), 0.4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text hyperlink
spannableString5.setSpan(new URLSpan("tel:123456789"), 5.14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the underscore
spannableString5.setSpan(new UnderlineSpan(), 15.19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the stripper
spannableString5.setSpan(new StrikethroughSpan(), 21.34, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Get the Drawable resource
Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
d.setBounds(0.0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
// Create an ImageSpan and replace the text with an ImageSpan
ImageSpan imgspan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
// Set the image
spannableString5.setSpan(imgspan, text5.length() - 1, text5.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv_content5.setText(spannableString5);
tv_content5.setMovementMethod(LinkMovementMethod.getInstance());
Copy the code

Need to implement

Requirement description: Input the content in the input box and submit it to the server. The server detects whether the submitted content is legal and returns the illegal statements in the form of collection. The client shows the illegal statements in red. Omit the network submission process, sensitive words are fixed content; To achieve the effect, as shown in the figure:Code implementation:

Default sensitive words:

/ / words
private List<String> words;

words = new ArrayList<>();
words.add("Too much");
words.add("Wonderful");
words.add("abc");
Copy the code

Find sensitive words and label them in red

private void setKeyWordColor(a) {
    if (words == null || words.size() < 0) {
        return;
    }
    Editable text = et_input.getText();
    if (text == null || TextUtils.isEmpty(text.toString())) {
        return;
    }
    String content = et_input.getText().toString();
    // Avoid endless loops
    if (content.equals(lastContent)) {
        return;
    }
    lastContent = content;

    SpannableStringBuilder builder = new SpannableStringBuilder(content);
    // Set the text to the initial color
    builder.setSpan(new ForegroundColorSpan(Color.parseColor("# 333333")), 0, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    for (int i = 0; i < words.size(); i++) {
        String str = words.get(i);
        if (TextUtils.isEmpty(str)) {
            continue;
        }
    
        if(! content.toLowerCase().contains(str.toLowerCase())) {continue;
        }
    
        // Find the sensitive word location
        int index = content.toLowerCase().indexOf(str.toLowerCase());
        while (index >= 0) {
            builder.setSpan(new ForegroundColorSpan(Color.parseColor("#EA5B4B")), index, index + str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); index = content.toLowerCase().indexOf(str.toLowerCase(), index + str.length()); }}int selectionStart = et_input.getSelectionStart();
    et_input.setText(builder);
    // Set the cursor to the previous position
    et_input.setSelection(selectionStart);
}

Copy the code

Input text detection, when the input of the same sensitive words, directly marked red:

et_input.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}@Override
    public void afterTextChanged(Editable s) {
        // Enter the same sensitive word, no longer request the interface, immediately marked redsetKeyWordColor(); }});Copy the code

The resources

The official document: developer. The android. Google. Cn/guide/topic… Novice tutorial: www.runoob.com/w3cnote/and…