Android实战--汉字转全拼

上一节介绍了天气预报小DEMO的制作,有些人就觉得打拼音不符合用户的习惯,怎么改成打汉字并且可以实现功能呢?这里就要用到汉字转全拼,这时我们就要引入jar包:

http://download.csdn.net/detail/yayun0516/8706403

将jar包解压后导入我们的工程:(导入方法自行百度)

Android实战--汉字转全拼_第1张图片

然后我们编写我们的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="南京" />

    <Button
        android:id="@+id/transfomer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="转换" />

    <TextView
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

编写工具类:

package com.lzg.hanzitopinyin;

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 HanZiToPinYin {
	/**
	 * 返回一个字的拼音
	 * @param hanzi
	 * @return
	 */
	public static String toPinYin(char hanzi){
		HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
	    hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	    hanyuPinyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//没有声调
	    hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
	    String[] pinyinArray=null;
	    try {
	    	//是否在汉字范围内
	    	if(hanzi>=0x4e00 && hanzi<=0x9fa5){
	    		//pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
	    		pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
	    	}
	    } catch (BadHanyuPinyinOutputFormatCombination e) {
	    	e.printStackTrace();
	    }
	    //将汉字返回
		return pinyinArray[0];
	}
}



编写activity:

package com.lzg.hanzitopinyin;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends Activity {
	/** Called when the activity is first created. */
	EditText editText;
	TextView tView;
	Button trbButton;
	String pinyin;
	String hanziString;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		editText = (EditText) findViewById(R.id.edit);
		tView = (TextView) findViewById(R.id.show);
		trbButton = (Button) findViewById(R.id.transfomer);
		trbButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				hanziString = editText.getText().toString();
				pinyin = getPinyin(hanziString);
				tView.setText("拼音:" + pinyin + "\n" + "汉字:" + hanziString);

			}
		});

	}

	private String getPinyin(String hanzi) {
		String pinyin = "";
		for (int i = 0; i < hanzi.length(); i++) {
			pinyin += HanZiToPinYin.toPinYin(hanzi.charAt(i));

		}
		return pinyin;
	}
}


 

运行实例如下:

Android实战--汉字转全拼_第2张图片

最后:

我的应用:                      http://openbox.mobilem.360.cn/index/d/sid/2966005

                                       http://android.myapp.com/myapp/detail.htm?apkName=com.yayun.gitlearning

欢迎下载,有问题多交流,讨论谢谢!

 

你可能感兴趣的:(android,api)