美文网首页
TextView关键字高亮显示

TextView关键字高亮显示

作者: 美晨菌 | 来源:发表于2020-09-05 13:40 被阅读0次
    public static SpannableString matcherSearchText(String text, String keyword) {
        SpannableString spannableString = new SpannableString(text);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(new SpannableString(text.toLowerCase()));
        // 如果需要高亮所有结果, 把if换成while
        if (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

相关文章

网友评论

      本文标题:TextView关键字高亮显示

      本文链接:https://www.haomeiwen.com/subject/tkcgsktx.html