在显示搜索结果的时候,我们一般会在结果中高亮显示搜索的关键字,为此好一阵搜索资料,不过找到方法后,却发现相当的简单,先上效果图:
设置方法:
<pre name="code" class="java">SpannableString wordToSpan = new SpannableString(title);
int start = title.indexOf(searchKeys);
int end = start + searchKeys.length();
wordToSpan.setSpan(new ForegroundColorSpan(Color.RED),
start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.productTitle.setText(wordToSpan);
其中的title是标题,searchKeys是搜索关键字,在ForegroundColor中,可以指定显示的颜色,或者使用BackgroundColor设置背景颜色,方法都是类似的,如果是在listview中使用该方法,直接在adapter中设置即可。