Pinyin4j只能算是Java开源类库,但是在Android的应用开发中也经常被用到。大家都用过手机的通讯录,通讯录的搜索联系人的功能,用pinyin4j就可以实现。下面我给大家带来这个例子,使用pinyin4j实现搜索联系人功能。
首先先对pinyin4j做一个简单的介绍:
pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,拼音的输出格式也可以定制,并且支持汉字的多音字。pinyin4j的官方网是:http://pinyin4j.sourceforge.net/
pinyin4j的使用手册,百度文库里有一个文档,大家要是想详细了解pinyin4j的使用,请点击pinyin4j的使用手册。
再继续讲之前,还是先带大家看一下本案例的效果图:
接下来咱们看一下工程目录:
本案例总共就两个类,其中PinyinTool是一个工具类,其代码如下:
package com.gc.testcontactpeopledemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; /** * * @author Android将军 * */ public class PinyinTool { public static HanyuPinyinOutputFormat format; public static List<Map<String,Map<String,String>>> list; public static Map<String,Map<String,String>> map; public static Map<String,String> pMap; public static List<String> strList; /** * 功能:根据联系人姓名生成拼音缩写与拼写全写 * @param strList 存储联系人姓名的List * @throws BadHanyuPinyinOutputFormatCombination */ public static void setData(List<String> strList) throws BadHanyuPinyinOutputFormatCombination { PinyinTool.strList=strList; format=new HanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); list=new ArrayList<Map<String,Map<String,String>>>(); for(int i=0;i<strList.size();i++) { map=new HashMap<String, Map<String,String>>(); pMap=new HashMap<String, String>(); pMap.put("simple", getSimple(strList.get(i))); pMap.put("complex", getComplex(strList.get(i))); map.put(strList.get(i), pMap); list.add(map); } } /** * 功能:获取字符串str的拼音缩写 * @param str * @return */ public static String getSimple(String str) { StringBuilder sb=new StringBuilder(); String tempSimple=null; for(int i=0;i<str.length();i++) { tempSimple=getCharacterSimple(str.charAt(i)); if(tempSimple==null) { sb.append(str.charAt(i)); }else { sb.append(tempSimple); } } return sb.toString(); } /** * 功能:获取字符C的拼音首字母 * @param c * @return */ public static String getCharacterSimple(char c) { String[] str=null; try { str=PinyinHelper.toHanyuPinyinStringArray(c, format); } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } if(str==null) return null; return str[0].charAt(0)+""; } public static String getComplex(String str) { StringBuilder sb=new StringBuilder(); String tempSimple=null; for(int i=0;i<str.length();i++) { tempSimple=getCharacterComplex(str.charAt(i)); if(tempSimple==null) { sb.append(str.charAt(i)); }else { sb.append(tempSimple); // System.out.println(sb.toString()); } } return sb.toString(); } public static String getCharacterComplex(char c) { String[] str=null; try { str=PinyinHelper.toHanyuPinyinStringArray(c, format); } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } if(str==null) return null; return str[0]; } /** * 功能:搜索符合条件的联系人 * @param str 当前联系人List * @return 新的联系人List */ public static List<String> search(String str) { List<String> temp=new ArrayList<String>(); for(int i=0;i<list.size();i++) { if(list.get(i).get(strList.get(i)).get("complex").contains(str)||list.get(i).get(strList.get(i)).get("simple").contains(str)) { System.out.println(strList.get(i)); temp.add(strList.get(i)); } } return temp; } }
下面再给出MainActivity的代码,如下:
package com.gc.testcontactpeopledemo; import java.util.ArrayList; import java.util.List; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; /** * * 功能:搜索联系人功能 * @author Android将军 * */ public class MainActivity extends Activity implements TextWatcher { private EditText mEditText; private ListView mListView; private PinyinHelper mPinyinHelper; private MyAdapter mMyAdapter; private List<String> mList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setData(); initUI(); setListeners(); } /** * 初始化联系人数据 */ public void setData() { mList=new ArrayList<String>(); mList.add("安徽"); mList.add("王小二"); mList.add("Android将军"); mList.add("天津大麻花"); mList.add("马场道"); mList.add("海河湾"); mList.add("五大道"); mList.add("苏州道"); mList.add("爱国道"); mList.add("天津图书大厦"); } /** * 初始化组件 */ public void initUI() { mEditText=(EditText)findViewById(R.id.search); mListView=(ListView)findViewById(R.id.listview); mMyAdapter=new MyAdapter(); mListView.setAdapter(mMyAdapter); try { PinyinTool.setData(mList); } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 设置监听 */ public void setListeners() { mEditText.addTextChangedListener(this); } /** * EditText监听事件,提供搜索功能 */ @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { for(int i=0;i<mList.size();i++) { } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub final String str=s.toString(); new Thread(){ @Override public void run() { mList=PinyinTool.search(str.toString()); Message message=new Message(); message.what=0; mHandler.sendMessage(message); } }.start(); } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what==0) { mMyAdapter.notifyDataSetChanged(); } } }; /** * 自定义Adapter,为ListView提供数据 * @author Chao Gong * */ public class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return mList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ItemHodlerView mItemHodlerView; if(convertView==null) { convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.listview_item, null); mItemHodlerView=new ItemHodlerView(); mItemHodlerView.mTextView=(TextView)convertView.findViewById(R.id.tv); convertView.setTag(mItemHodlerView); } else { mItemHodlerView=(ItemHodlerView)convertView.getTag(); } mItemHodlerView.mTextView.setText(mList.get(position)); mItemHodlerView.mTextView.setTag(position); return convertView; } } private class ItemHodlerView { private ImageView mImageView; private TextView mTextView; } }
由于代码比较简单再加上有部分注释,故在此就不做过多的解释了,如果还有不明白处,回复留言即可。
转载请注明出处:http://blog.csdn.net/android_jiangjun/article/details/40422251
时间: 2024-10-10 21:32:59