当前位置:   article > 正文

Android 判断字符串是否包含指定字符_android 判断字符串是否包含指定字符串

android 判断字符串是否包含指定字符串

搜索这个功能。
需求是对搜索到的指定字符进行高亮显示。
效果如下:

 public SpannableString matcherSearchText(String text, String keyword) {
        SpannableString ss = new SpannableString(text);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new TextAppearanceSpan(getActivity(), com.android.settings.R.style.serach_color_style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//new ForegroundColorSpan(color)
        }
        return ss;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  SpannableString spannableString =fragment.matcherSearchText(result.title.toString(), fragment.getQuery());
  titleView.setText(spannableString);
  • 1
  • 2
<style name="serach_color_style">
        <item name="android:textColor">#579DFF</item>
</style>
  • 1
  • 2
  • 3

上面的serach_color_style ,设一个你想要的颜色值就可以了。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号