您的位置:首页 > 其它

设置搜索关键字高亮

2015-11-30 17:23 190 查看
/**
     * 设置搜索关键字高亮
     * @param content 原文本内容
     * @param keyword 关键字
     */  
    private SpannableString setKeyWordColor(String content,String keyword){  
        SpannableString s = new SpannableString(content);  
        Pattern p = Pattern.compile(keyword);  
        Matcher m = p.matcher(s);  
        while (m.find()){  
            int start = m.start();  
            int end = m.end();  
            s.setSpan(new ForegroundColorSpan(Color.RED),start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
        }  
        return s;  
    }  

//显示效果
你好吗?
 textView.setText(setKeyWordColor("你好吗?","好"));  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: