Android通讯录开发之取得姓名首字母实现简拼搜索

原文

2013年12月27日 开发日志

目前小巫在实习的公司,负责一个项目的开发,虽说是接手过来的,不过经过前面的几位实习生哥们推敲之后,轮到我的手里,我只能说我很好运,捡到了一个几乎需要重构的项目,我接手开发一个月,已经提交了5、6个测试版本,问题一堆,我改代码改得我想吐。说起这个项目一个重要模块就是通讯录这一块,前面的几个大哥能把通讯录做得那么烂也是他们的本事,我几乎是在重做这一个模块,增加了很多新特性,全选、删除,模糊匹配等,连读数据库我也是从新找了个新的方法进行了优化。经过这段时间的推敲,项目总算是有了好转,我刚开始也是磕磕碰碰得改,也是自己项目经验不足,很多细节问题没有注意到,说到底还是对用户体验这一块体会不深,这也只能慢慢修炼了。

之前发了一篇关于模糊匹配搜索的,好像自己的那一块没有实现首字母简拼的匹配,本篇博客也是介绍这一块。

http://blog.csdn.net/leayefang/article/details/9082255得到一个好用的工具类,专门用来获取中文首字母的。

效果图:


  1. package com.suntek.mobilemeeting.utils;  
  2.   
  3. /** 
  4.  * 2013-12-27 
  5.  *  
  6.  * @author wwj 
  7.  *  
  8.  */  
  9. public class FirstLetterUtil {  
  10.     private static int BEGIN = 45217;  
  11.     private static int END = 63486;  
  12.     // 按照声母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。  
  13.     // i, u, v都不做声母, 自定规则跟随前面的字母  
  14.     private static char[] chartable = { '啊''芭''擦''搭''蛾''发''噶''哈',  
  15.             '哈''击''喀''垃''妈''拿''哦''啪''期''然''撒''塌''塌',  
  16.             '塌''挖''昔''压''匝', };  
  17.     // 二十六个字母区间对应二十七个端点  
  18.     // GB2312码汉字区间十进制表示  
  19.     private static int[] table = new int[27];  
  20.     // 对应首字母区间表  
  21.     private static char[] initialtable = { 'a''b''c''d''e''f''g',  
  22.             'h''h''j''k''l''m''n''o''p''q''r''s''t',  
  23.             't''t''w''x''y''z', };  
  24.   
  25.     // 初始化  
  26.     static {  
  27.         for (int i = 0; i < 26; i++) {  
  28.             table[i] = gbValue(chartable[i]);// 得到GB2312码的首字母区间端点表,十进制。  
  29.         }  
  30.         table[26] = END;// 区间表结尾  
  31.     }  
  32.   
  33.     /** 
  34.      * 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 最重要的一个方法,思路如下:一个个字符读入、判断、输出 
  35.      */  
  36.     public static String getFirstLetter(String sourceStr) {  
  37.         String result = "";  
  38.         String str = sourceStr.toLowerCase();  
  39.         int StrLength = str.length();  
  40.         int i;  
  41.         try {  
  42.             for (i = 0; i < StrLength; i++) {  
  43.                 result += Char2Initial(str.charAt(i));  
  44.             }  
  45.         } catch (Exception e) {  
  46.             result = "";  
  47.         }  
  48.         return result;  
  49.     }  
  50.   
  51.     /** 
  52.      * 输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '0' 
  53.      */  
  54.     private static char Char2Initial(char ch) {  
  55.         // 对英文字母的处理:小写字母转换为大写,大写的直接返回  
  56.         if (ch >= 'a' && ch <= 'z') {  
  57.             return ch;  
  58.         }  
  59.         if (ch >= 'A' && ch <= 'Z') {  
  60.   
  61.             return ch;  
  62.         }  
  63.         // 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内,  
  64.         // 若不是,则直接返回。  
  65.         // 若是,则在码表内的进行判断。  
  66.         int gb = gbValue(ch);// 汉字转换首字母  
  67.   
  68.         if ((gb < BEGIN) || (gb > END))// 在码表区间之前,直接返回  
  69.         {  
  70.             return ch;  
  71.         }  
  72.   
  73.         int i;  
  74.         for (i = 0; i < 26; i++) {// 判断匹配码表区间,匹配到就break,判断区间形如“[,)”  
  75.             if ((gb >= table[i]) && (gb < table[i + 1])) {  
  76.                 break;  
  77.             }  
  78.         }  
  79.   
  80.         if (gb == END) {// 补上GB2312区间最右端  
  81.             i = 25;  
  82.         }  
  83.         return initialtable[i]; // 在码表区间中,返回首字母  
  84.     }  
  85.   
  86.     /** 
  87.      * 取出汉字的编码 cn 汉字 
  88.      */  
  89.     private static int gbValue(char ch) {// 将一个汉字(GB2312)转换为十进制表示。  
  90.         String str = new String();  
  91.         str += ch;  
  92.         try {  
  93.             byte[] bytes = str.getBytes("GB2312");  
  94.             if (bytes.length < 2) {  
  95.                 return 0;  
  96.             }  
  97.             return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);  
  98.         } catch (Exception e) {  
  99.             return 0;  
  100.         }  
  101.     }  
  102. }  

// 搜索的方法,增加简拼搜索

  1. /** 
  2.      * 按号码-拼音搜索联系人 
  3.      *  
  4.      * @param str 
  5.      */  
  6.     public static ArrayList<Contact> search(String str,  
  7.             ArrayList<Contact> allContacts, ArrayList<Contact> contactList) {  
  8.         contactList.clear();  
  9.         // 如果搜索条件以0 1 +开头则按号码搜索  
  10.         if (str.startsWith("0") || str.startsWith("1") || str.startsWith("+")) {  
  11.             for (Contact contact : allContacts) {  
  12.                 if (contact.getNumber() != null && contact.getName() != null) {  
  13.                     if (contact.getNumber().contains(str)  
  14.                             || contact.getName().contains(str)) {  
  15.                         contact.setGroup(str);  
  16.                         contactList.add(contact);  
  17.                     }  
  18.                 }  
  19.             }  
  20.             return contactList;  
  21.         }  
  22.   
  23.         boolean isChinese = false;  
  24.         Pattern pattern = Pattern.compile("[\\u4E00-\\u9FA5]");  
  25.         Matcher matcher = pattern.matcher(str);  
  26.         if (matcher.find()) { // 如果是中文  
  27.             isChinese = true;  
  28.         }  
  29.   
  30.         for (Contact contact : allContacts) {  
  31.             if (contains(contact, str, isChinese)) {  
  32.                 contactList.add(contact);  
  33.             } else if (contact.getNumber().contains(str)) {  
  34.                 contact.setGroup(str);  
  35.                 contactList.add(contact);  
  36.             }  
  37.         }  
  38.         return contactList;  
  39.     }  
  40.   
  41.     /** 
  42.      * 根据拼音搜索 
  43.      *  
  44.      * @param str 
  45.      *            正则表达式 
  46.      * @param pyName 
  47.      *            拼音 
  48.      * @param isIncludsive 
  49.      *            搜索条件是否大于6个字符 
  50.      * @return 
  51.      */  
  52.     public static boolean contains(Contact contact, String search,  
  53.             boolean isChinese) {  
  54.         if (TextUtils.isEmpty(contact.getName())) {  
  55.             return false;  
  56.         }  
  57.   
  58.         boolean flag = false;  
  59.         if (isChinese) {  
  60.             // 根据全拼中文查询  
  61.             Pattern pattern = Pattern.compile(search.replace("-"""),  
  62.                     Pattern.CASE_INSENSITIVE);  
  63.             Matcher matcher = pattern.matcher(contact.getName());  
  64.             if (flag) {  
  65.                 contact.setGroup(matcher.group());  
  66.             }  
  67.             return matcher.find();  
  68.         }  
  69.   
  70.         // 简拼匹配,如果输入在字符串长度大于6就不按首字母匹配了  
  71.         if (search.length() < 6) {  
  72.             String firstLetters = FirstLetterUtil.getFirstLetter(contact  
  73.                     .getName());  
  74.             Pattern firstLetterMatcher = Pattern.compile(search.toLowerCase(),  
  75.                     Pattern.CASE_INSENSITIVE);  
  76.             return firstLetterMatcher.matcher(firstLetters).find();  
  77.         }  
  78.   
  79.         // 全拼匹配  
  80.         ChineseSpelling finder = ChineseSpelling.getInstance();  
  81.         finder.setResource(contact.getName());  
  82.         Pattern pattern2 = Pattern.compile(search.toUpperCase(),  
  83.                 Pattern.CASE_INSENSITIVE);  
  84.         Matcher matcher2 = pattern2.matcher(finder.getSpelling());  
  85.         flag = matcher2.find();  
  86.   
  87.         return flag;  
  88.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值