PINYIN4J is only a Java open source class library, but it is often used in Android application development. Everyone has used the phone's address book, contacts of the search contact function, with pinyin4j can be achieved. Here I give you this example, using pinyin4j to implement the search contact function.
First, a simple introduction to pinyin4j:
Pinyin4j is a popular Java library, supporting the conversion between Chinese characters and pinyin, pinyin output format can also be customized, and support the polyphone of Chinese characters. The official web of PINYIN4J is:http://pinyin4j.sourceforge.net/
PINYIN4J Manual, Baidu Library has a document, if you want to learn more about the use of pinyin4j, please click on the pinyin4j user manual.
Before we go on, let's take a look at this case:
Let's take a look at the project catalog:
This case has a total of two classes, where Pinyintool is a tool class with the following code:
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 General * * /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;/** * function: Generate phonetic initials and spell-write based on contact name * @param strlist store Contact name Lis T * @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);}} /** * Function: Gets the phonetic abbreviation of the string str * @param str * @return */public static string getsimple (String str) {StringBuilder sb=new Stringbuil Der (); String tempsimple=null;for (int i=0;i<str.length (); i++) {tempsimple=getcharactersimple (Str.charat (i)); Tempsimple==null) {sb.append (Str.charat (i));} Else{sb.append (Tempsimple);}} return sb.tostring ();} /** * Function: Get the character C of the pinyin first letter * @param c * @return */public static String getcharactersimple (char c) {string[] Str=null;try {str=pin Yinhelper.tohanyupinyinstringarray (c, format);} catch (Badhanyupinyinoutputformatcombination e) {//TODO auto-generated catch Blocke.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)); 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 Blocke.printstacktrace ();} if (str==null) return Null;return str[0];} /** * Features: Search for eligible contacts * @param str Current CONTACT list * @return new contact list */public static list<string> search (String str) {list& Lt 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;}}The following gives the mainactivity code, as follows:
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;/** * Features: Search contacts function * @author Android General * */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 (); }/** * Initialize contact data */public void SetData () {mlist=new arraylist<string> (); Mlist.add ("Anhui"); Mlist.add ("Wangxiao"); Mlist.add ("Android General"); Mlist.add ("Tianjin Big Twist"); Mlist.add ("Racecourse Road"); Mlist.add ("Haihe wan"); Mlist.add ("Five Avenue"); Mlist.add ("Suzhou Road"); Mlist.add ("Patriotic Way"); Mlist.add ("Tianjin Book Building"); }/** * Initialize components */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 Blocke.printstacktrace ();} }/** * Set monitor */public void Setlisteners () {Medittext.addtextchangedlistener (this); } /** * EditText Listener event, provides search function */@Overridepublic void beforetextchanged (charsequence s, int start, int count,int afte R) {for (int i=0;i<mlist.size (); i++) {}} @Overridepublic void OnTextChanged (charsequence s, int start, int before, int co UNT) {//TODO auto-generated method stubfinal String str=s.tostring (); new Thread () {@Overridepublic void run () {Mlist=piny Intool.search (Str.tostring ()); Message message=new message (); message.what=0;mhandler.sendmessage (message);}}. Start ();} @Overridepublic void aftertextchanged (Editable s) {//TODO auto-generated method Stub}handler mhandler=new Handler () {@ overridepublic void Handlemessage (Message msg) {//TODO auto-generated method stubif (msg.what==0) { Mmyadapter.notifydatasetchanged ();}}};/ * * Custom adapter, provide data for ListView * @author Chao Gong * */public class Myadapter extends baseadapter{@Overridepublic int Getco UNT () {//TODO auto-generated method Stubreturn mlist.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method stubreturn Mlist.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic 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;}
Because the code is simple and some comments, so there is not too much explanation, if there is not clear, reply message can be.
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/40422251
Android Open source class library pinyin4j use----search for contacts