过滤搜索条件
http://download.csdn.net/tag/android%E5%BF%AB%E9%80%9F%E6%BB%91%E5%8A%A8%E5%88%97%E8%A1%A8%E3%80%81%E9%80%9A%E8%AE%AF%E5%BD%95
http://download.csdn.net/detail/jiong056/3903023
http://gundumw100.iteye.com/blog/1331258
http://www.iteye.com/topic/135806
http://blog.csdn.net/ilysony/article/details/6292771
http://www.cnblogs.com/cnsanshao/archive/2011/08/19/2145210.html
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=81435&extra=page%3D3%26filter%3Dtypeid%26typeid%3D5%26typeid%3D5
android 联系人表中有一个字段sort_key 大概是朱强 : ZHU 朱 QIANG 强
搜索策略 一般就是支持简拼、全拼、中文、号码和拨号盘
提供一种方法可以把简拼、全拼、中文可以放在一起处理,先把 “ZHU 朱 QIANG 强”
变成“ZHU 朱 QIANG 强 ZQ” 用下面的方法就可以一起处理了。
public class LongMain{ private static String sourceStr = "朱 ZHU 强 QIANG ZQ"; private static String key = "朱强"; private static String reg = "(.*\\b)?$z"; // 优化:charAt()效率,StringBuffer public static void main(String[] args) { String regex = ""; key = key.toUpperCase(); for (int i = 0; i < key.length(); i++) { regex += reg.replace("$z", String.valueOf(key.charAt(i))); } regex = "^" + regex + ".*$"; System.out.println(Pattern.compile(regex).matcher(sourceStr).matches()); }}
结果应该是 true
key换成是 ZQ、ZHUQIANG、Z、Q、朱、强 应该都是true 大概是满足搜索的过滤策略了
过滤完 以后可能还会涉及到关键字高亮的问题
下面提供3中方式:
1、第一种
Spanned style = Html.fromHtml(phone.replaceAll(input, "<font color='red'>" + input + "</font>"));number_tv.setText(style);
2、第二种
int index = phone.indexOf(input);int len = input.length();Spanned temp = Html.fromHtml(phone.substring(0, index) + "<font color=red>" + phone.substring(index, index + len) + "</font>" + phone.substring(index + len, phone.length()));number_tv.setText(temp);
3、第三种
int start = phone.indexOf(input); SpannableStringBuilder style=new SpannableStringBuilder(phone); style.setSpan(new ForegroundColorSpan(Color.RED), start, start + input.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);number_tv.setText(style);