TextView 关键字索引 关键字高亮

String.valueOf(str.indexOf("三"));

引用:http://www.cnblogs.com/gyflyx/archive/2011/07/09/2101912.html

/** 

     * 关键字高亮显示 

     * @param target 需要高亮的关键字 

     */  

    public void highlight(String target, TextView textView){  

        String temp=textView.getText().toString();  

        SpannableStringBuilder spannable = new SpannableStringBuilder(temp);  

        CharacterStyle span=null;  

          

        Pattern p = Pattern.compile(target);  

        Matcher m = p.matcher(temp);  

        while (m.find()) {  

            span = new ForegroundColorSpan(Color.RED);//需要重复!

          //span = new ImageSpan(drawable,ImageSpan.XX);//设置现在图片

            spannable.setSpan(span, m.start(), m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  

        }  

        textView.setText(spannable);  

    }  

原文地址:https://www.cnblogs.com/sode/p/2174460.html