通讯录(汉字转拼音)

通讯录打开后,触屏到某个字母开头的姓就能很方便快捷地找到联系人。所以如何知道姓名开头字母呢,这里我用了一个获取中文的拼音包。


从测试类开始吧:

import java.util.*;

public class Test {

    List<People> phoneBook =new ArrayList<People>();

    //测试
	public static void main(String[] args) {
		
		Test t=new Test();
		t.init();
		t.sortPeople();
		t.print();
		
	}
	
	//方法三:排序处理
	private void sortPeople(){
		Set<String> first=new HashSet<String>();
		for( People p : phoneBook) {
			first.add(""+p.getJx().charAt(0));
		}
		
		for (String c : first) {
			System.out.println("----------"+c);
			for( People p : phoneBook) {
				if(c.equals(""+p.getJx().charAt(0))){
					System.out.println(p);
				}
			}
		}
	}
	
	//方法二: 输出
	private void print(){
		for (People p : phoneBook) {
			System.out.println(p);
		}
	}
	
	//方法一:准备
	private void init() {
		String names[]={"廖培鑫","刘新","叶涛","邱高颖","张斌"};   //存到文本中
		People p=null;
		for (int i = 0; i < names.length; i++) {
			p=new People();
			p.setName(names[i]);
			p.setJx(GetPinyin.getPinYinHeadChar(names[i]));
			//号码
			ArrayList<String> phone=new ArrayList<String>();
			phone.add("1591918492"+i);
			phone.add("1367605560"+i);
			p.setPhone(phone);
			phoneBook.add(p);
		}
	}
}

所用到拼音工具的类:

package pinyin;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
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;

public class GetPinyin {

    /**
     * 得到 全拼
     * @param src
     * @return
     */
    public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    t4 += java.lang.Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();t4 = "#";
        }
        return t4;
    }

    /**
     * 得到中文首字母
     * 
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

    /**
     * 将字符串转移为ASCII码
     * 
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            // System.out.println(Integer.toHexString(bGBK[i]&0xff));
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }

    public static void main(String[] args) {
        String cnStr = "邱高颖";
        System.out.println(getPingYin(cnStr));
        System.out.println(getPinYinHeadChar(cnStr));
    }
}

所用到的Bean :

package pinyin;

import java.util.ArrayList;

/**
 * 联系人
 * @author 小高
 */
public class People {

	private String jx;  //拼音简写
	private String name; //联系人
	private ArrayList<String> phone; //一个联系人可能有多个号码

	public People(String jx, String name, ArrayList<String> phone) {
		super();
		this.jx = jx;
		this.name = name;
		this.phone = phone;
	}

	public People() {
		super();
	}

	public String getJx() {
		return jx;
	}
	public void setJx(String jx) {
		this.jx = jx;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ArrayList<String> getPhone() {
		return phone;
	}
	public void setPhone(ArrayList<String> phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "People [jx=" + jx + ", name=" + name + ", phone=" + phone + "]";
	}
}

运行效果:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>