android-关键字标红-SpannableString

1.工具类

KeywordUtil

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.SubscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;

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

/**
 *
 **/
public class KeywordUtil {
    public static SpannableString mSpannableString(int color,String text,String [] keyword){
        SpannableString s=new SpannableString(text);
        for (int a=0;a<keyword.length;a++){
            //正则
            Pattern p=Pattern.compile(keyword[a]);
            //获取结果
            Matcher m=p.matcher(s);
            while (m.find()){
                int start=m.start();
                int end=m.end();
//                SPAN_EXCLUSIVE_EXCLUSIVE不包含起始位置和结束位置  只是中间位置
//                设置关键字字体颜色
//                方法1
//                s.setSpan(
//                        new UnderlineSpan(){
//                            public void updateDrawState(TextPaint ds) {
//                                ds.setColor(Color.RED);
//                                ds.setUnderlineText(false);//去掉下划线
//                            }
//                        }
//                        ,start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//                方法2
                s.setSpan(new ForegroundColorSpan(Color.RED)
                        ,start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                s.setSpan(new ForegroundColorSpan(Color.RED), 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                //设置背景颜色
                s.setSpan(new BackgroundColorSpan(Color.CYAN), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            }
        }
        return s;
    }
}

2.activity


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.SpannableString;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
//所有内容
    String text="我不是刘德华不给你唱忘情水\n" +
        "\n" +
        "也不是张学友我不给你唱吻别\n" +
        "\n" +
        "但我像郭富城对你爱不完\n" +
        "\n" +
        "变成黎明笑着给你唱无名份的浪漫\n" +
        "\n" +
        "我不是刘德华不给你唱忘情水\n" +
        "\n" +
        "也不是张学友我不给你唱吻别\n" +
        "\n" +
        "但我像郭富城对你爱不完\n" +
        "\n" +
        "变成黎明笑着给你唱无名份的浪漫\n" +
        "\n" +
        "卢克:\n" +
        "\n" +
        "我不是刘德华\n" +
        "\n" +
        "但相信我说的话\n" +
        "\n" +
        "没感觉是假的吧\n" +
        "\n" +
        "我不敢去想象没你的未来\n" +
        "\n" +
        "许愿你一直在\n" +
        "\n" +
        "我总是没有时间跑得快\n" +
        "\n" +
        "但我还是那个努力跑的男孩\n" +
        "\n" +
        "站在公交站牌\n" +
        "\n" +
        "和你邂逅的14 路怎么还没来\n" +
        "\n" +
        "备忘录里的歌词我删了又再改\n" +
        "\n" +
        "告诉我你在哪里\n" +
        "\n" +
        "男孩的日记翻开\n" +
        "\n" +
        "女孩的心思难猜\n" +
        "\n" +
        "如果我有超能力那就是超喜欢你\n" +
        "\n" +
        "所以别往后退我真的没力再追\n" +
        "\n" +
        "So 方向一路向北\n" +
        "\n" +
        "新地:\n" +
        "\n" +
        "我不是刘德华不给你唱忘情水\n" +
        "\n" +
        "也不是张学友我不给你唱吻别\n" +
        "\n" +
        "但我像郭富城对你爱不完\n" +
        "\n" +
        "变成黎明笑着给你唱无名份的浪漫\n" +
        "\n" +
        "我不是刘德华不给你唱忘情水\n" +
        "\n" +
        "也不是张学友我不给你唱吻别\n" +
        "\n" +
        "但我像郭富城对你爱不完\n" +
        "\n" +
        "变成黎明笑着给你唱无名份的浪漫\n" +
        "\n" +
        "我不明白为什么一定要分开\n" +
        "\n" +
        "Dont you lie to me 我没有办法忍耐\n" +
        "\n" +
        "最后的对白\n" +
        "\n" +
        "我宁愿你是骗我的\n" +
        "\n" +
        "你要的我全都有了\n" +
        "\n" +
        "就当你欠我的\n" +
        "\n" +
        "玩笑开太多干脆你我都别过\n" +
        "\n" +
        "被你结了冰的心脏一下就戳破\n" +
        "\n" +
        "所有女孩往后靠写日记得先排号\n" +
        "\n" +
        "到底爱谁不知道让姑娘一见我就笑\n" +
        "\n" +
        "卢克:\n" +
        "\n" +
        "我不是刘德华不给你唱忘情水\n" +
        "\n" +
        "也不是张学友我不给你唱吻别\n" +
        "\n" +
        "但我像郭富城对你爱不完\n" +
        "\n" +
        "变成黎明\n" +
        "\n" +
        "卢克:\n" +
        "\n" +
        "我说哎呀哎呀\n" +
        "\n" +
        "可不可以让我有个可以完全放松的角落\n" +
        "\n" +
        "放松的角落有许多许多糖果还有\n" +
        "\n" +
        "童话故事拜托竖起耳朵\n" +
        "\n" +
        "现在一切纯属意外\n" +
        "\n" +
        "丘比特夸我是个天才\n" +
        "\n" +
        "因为你的唠叨无处不在\n" +
        "\n" +
        "奶茶半糖走冰\n" +
        "\n" +
        "微信回复还必须快呀\n" +
        "\n" +
        "新地:\n" +
        "\n" +
        "我不是刘德华不给你唱忘情水\n" +
        "\n" +
        "也不是张学友我不给你唱吻别\n" +
        "\n" +
        "但我像郭富城对你爱不完\n" +
        "\n" +
        "变成黎明笑着给你唱无名份的浪漫\n" +
        "\n" +
        "我不是刘德华\n" +
        "\n" +
        "但相信我说的话\n" +
        "\n" +
        "没感觉是假的\n" +
        "\n" +
        "加入银河计划点亮星光\n" +
        "\n" +
        "以上就是关于我不是刘德华不给你唱忘情水什么歌的介绍了。希望这些内容能对大家有所帮助。更多的精彩内容请关注游侠手游,这里一直会更新最新的手游攻略。";
//    显示文字孔家
    private TextView textView;
//    关键字
    private String [] key={"刘德华","你","我","唱"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SpannableString spannableString=KeywordUtil.mSpannableString(R.color.read,
                text,key
                );
        textView=findViewById(R.id.keytxt);
        textView.setText(spannableString);
    }
}

3.布局界面

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/keytxt"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

KeyText(关键字标红).rar-互联网文档类资源-CSDN下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值