Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

android / dayu

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • dayu
  • ..
  • widget
  • DiyStyleTextView.java
Find file
Normal viewHistoryPermalink
DiyStyleTextView.java 2.83 KB
Newer Older
luofan's avatar
集成图形验证码lib
09b81edf
 
luofan committed 4 years ago
1 2 3 4 5 6 7 8 9 10 11 12
package com.example.verificationcodejavademo.widget;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
wukun's avatar
1.androidx
831aeac7
 
wukun committed 2 years ago
13
import androidx.appcompat.widget.AppCompatTextView;
luofan's avatar
集成图形验证码lib
09b81edf
 
luofan committed 4 years ago
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Date:2020/5/19
 * author:wuyan
 */
public class DiyStyleTextView extends AppCompatTextView {

    private String colorRegex = "";//需要改变颜色的内容
    private int color = 0;//需要改变的颜色
    private ArrayList<Integer> indexArr = new ArrayList<>();
    private ArrayList<String> strArr = new ArrayList<>();

    public DiyStyleTextView(Context context) {
        super(context);
    }

    public DiyStyleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置需要改变颜色的文本,和改变的颜色
     */
    private DiyStyleTextView setColorRegex(String colorRegex, int color) {
        this.colorRegex = colorRegex;
        this.color = color;
        return this;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(setText(text, false), type);
    }

    private CharSequence setText(CharSequence text, Boolean flag) {
        if (TextUtils.isEmpty(text)) {
            if (flag) super.setText(text);
            return text;
        }
        SpannableStringBuilder styledText = new SpannableStringBuilder(text);
        if (!TextUtils.isEmpty(colorRegex)) {
            indexArr.clear();
            strArr.clear();
            Pattern p = Pattern.compile(colorRegex);
            Matcher m = p.matcher(text);
            while (m.find()) {
                strArr.add(m.group());
                indexArr.add(m.start());
            }
            for (int i = 0; i < indexArr.size(); i++) {
                int index = indexArr.get(i);
                String clickText = strArr.get(i);

                styledText.setSpan(
                        new TextViewClickSpan(clickText),
                        index,
                        index + clickText.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                );
            }
        }
        if (flag) super.setText(styledText);
        return styledText;
    }

    private class TextViewClickSpan extends ClickableSpan {

        public TextViewClickSpan(String clickText) {
        }

        @Override
        public void onClick(@NonNull View widget) {

        }

        @Override
        public void updateDrawState(@NonNull TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(color);
        }
    }
}