The previous section introduces the production of the Weather forecast small demo, some people think that playing pinyin does not conform to the user's habit, how to change to play Chinese characters and can realize the function? Here we need to use the Chinese characters to complete the spell, then we will introduce the JAR package:
http://download.csdn.net/detail/yayun0516/8706403
Unpack the jar package and import it into our project: (Import method Self-Baidu)
Then we write our layout file:
<?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:o rientation= "vertical" > <edittext android:id= "@+id/edit" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Nanjing"/> <button android:id= "@+id/ Transfomer " android:layout_width=" fill_parent " android:layout_height=" Wrap_content " android: text= "Convert"/> <textview android:id= "@+id/show " android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= ""/></linearlayout>
To write a tool class:
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 {/** * returns pinyin for a word * @param hanzi * @return */public static String topinyin (char hanzi) {Hanyupinyinoutputformat Hanyupinyin = new Hanyupinyinoutputformat (); Hanyupinyin.setcasetype (hanyupinyincasetype.lowercase); Hanyupinyin.settonetype (Hanyupinyintonetype.without_tone);//No Tone Hanyupinyin.setvchartype ( Hanyupinyinvchartype.with_u_unicode); String[] Pinyinarray=null; try {//whether within the Kanji range if (hanzi>=0x4e00 && hanzi<=0x9fa5) {//pinyinarray = Pinyinhelper.tohanyupinyinstri Ngarray (Hanzi, Hanyupinyin); Pinyinarray = Pinyinhelper.tohanyupiNyinstringarray (Hanzi, Hanyupinyin); }} catch (Badhanyupinyinoutputformatcombination e) {e.printstacktrace (); }//returning Chinese characters to return pinyinarray[0];}}
Writing 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; @Overridepublic 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 () {@ overridepublic void OnClick (View arg0) {hanzistring = Edittext.gettext (). toString ();p Inyin = Getpinyin (hanzistring); Tview.settext ("pinyin:" + pinyin + "\ n" + "Kanji:" + hanzistring);});} private string Getpinyin (String hanzi) {string pinyin = ""; for (int i = 0; i < hanzi.length (); i++) {pinyin + Hanzitopinyin.topinyin (Hanzi.charat (i));} return pinyin;}}
Run the following example:
At last:
My application: http://openbox.mobilem.360.cn/index/d/sid/2966005
http://android.myapp.com/myapp/detail.htm?apkName=com.yayun.gitlearning
Welcome to download, have the problem of more communication, discussion thank you!
android--Chinese characters to full spell