I previously worked on the address book software, where I had been worried about how to implement T9 input while I was working on a dial. I found a lot of posts online and I was not satisfied with the answer.
However, it was finally implemented. It seems that many friends in the community need it. Let's share it here.
This is the dial case extracted from my project. It fully implements the dial function in other address books, but it still lacks loading efficiency. Please kindly advise.
There is a picture with the truth:
At present, I use a 1G Single-core cpu Sony Ericsson mt15i testing machine, more than 1500 contacts, loading time is about 8 seconds, of course, the average user does not have so many contacts.
Since the program I made was either a dial disk or an interface at the beginning, I asynchronously loaded contacts at the beginning of the program in the background,
When the dial page is displayed, the contact loading is almost complete.
However, I think it is a little slow to read the contact data at the beginning of the program and sort them into the data needed for t9 search,
It is best to create a table to store it, and then listen to the changes in the contact table to dynamically update t9 data.
If you have better and faster ideas, I hope you can share them!
Some code:
Copy codeThe Code is as follows: public class ToPinYin {
/**
* Convert the passed Chinese Character list to the pinyin List
* @ Param list
*/
Public static List <String> getPinyinList (List <String> list ){
List <String> pinyinList = new ArrayList <String> ();
For (Iterator <String> I = list. iterator (); I. hasNext ();){
String str = (String) I. next ();
Try {
String pinyin = getPinYin (str );
PinyinList. add (pinyin );
} Catch (BadHanyuPinyinOutputFormatCombination e ){
E. printStackTrace ();
}
}
Return pinyinList;
}
/**
* Convert Chinese to PinYin
* @ Param pinyin-Chinese Characters
* @ Return
*/
Public static String getPinYin (String zhongwen)
Throws BadHanyuPinyinOutputFormatCombination {
String zhongWenPinYin = "";
Char [] chars = zhongwen. toCharArray ();
For (int I = 0; I <chars. length; I ++ ){
String [] pinYin = PinyinHelper. toHanyuPinyinStringArray (chars [I], getDefaultOutputFormat ());
// If the conversion is not a Chinese character, null is returned.
If (pinYin! = Null ){
ZhongWenPinYin + = pinYin [0];
} Else {
ZhongWenPinYin + = chars [I];
}
}
Return zhongWenPinYin;
}
/**
* Output format
*
* @ Return
*/
Private static HanyuPinyinOutputFormat getDefaultOutputFormat (){
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat ();
Format. setCaseType (HanyuPinyinCaseType. UPPERCASE); // UPPERCASE
Format. setToneType (HanyuPinyinToneType. WITHOUT_TONE); // No tone number
Format. setVCharType (HanyuPinyinVCharType. WITH_U_AND_COLON); // u display
Return format;
}