19_ the conversion of Chinese characters and stitching using the Hanzitopinyin tools provided by Android

Source: Internet
Author: User

The effect of converting Chinese characters into stitching has many implementations, such as PINYIN4J is a popular Java library that supports conversion between Chinese characters and pinyin. The Pinyin output format can be customized (the following blog will detail how to use this). But in the Android System application contacts also give us the implementation of the Chinese character and splicing conversion mode, next we briefly introduce the Android Chinese and pinyin conversion.

1. First go to the https://github.com/android website to download Android /platform_packages_ with Contact interface Providers_contactsprovider

Click DownLoad ZIP to download

2. Find Platform_packages_providers_contactsprovider-master\src\com\android\providers\contacts The Hanzitopinyin.java in this directory are copied to the project, and you will find that this class relies on the Transliterator.java class in the underlying, so also download this class, below the source code for this class is as follows:

/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package libcore.icu;/** * Exposes icu4c ' s transliterator.  */public Final class Transliterator {private long peer;   /** * Creates a new transliterator for the given ID.  */Public Transliterator (String ID) {peer = Create (ID);      } @Override protected synchronized void Finalize () throws Throwable {try {destroy (peer);    Peer = 0;    } finally {super.finalize ();  }}/** * Returns The IDs of all known transliterators.  */public static native string[] Getavailableids ();   /** * Transliterates the specified string.  */Public String transliterate (string s) {return transliterate (peer, s);  } private static native long create (String ID);  private static native void Destroy (long peer); private static native string transliterate (long peer, string s);}


3. Use a case description below

3.1 Effects are as follows

Note 1. Enter Chinese characters in the input box, click the Convert button to see the effect

4. Directory Structure of the project

This directory can see that the classes in the Com.example.util and LIBCORE.ICU packages are code that introduces the Android system, and then we'll look at the implementation of Mainactivity.java.

5. First take a look at the layout file for the mainactivity corresponding interface

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= "${relativepackage}.${activityclass}" >    < EditText        android:id= "@+id/et_content"        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "/>    <textview        android:id=" @+id/tv_content "android:layout_width=" Match_parent "        android:layout_height= "wrap_content"         android:layout_margintop= "20DP"        android:layout_ Marginbottom= "20DP"        android:text= "@string/hello_world"/>    <button        android:layout_width= " Match_parent "        android:layout_height=" wrap_content "        android:onclick=" ClickView "        android:text=" conversion "/></linearlayout>


Implementation in 6.MainActivity

Package Com.example.pinyin;import Android.app.activity;import Android.os.bundle;import android.text.TextUtils; Import Android.view.view;import android.widget.edittext;import Android.widget.textview;import android.widget.Toast Import Com.example.util.hanzitopinyin;public class Mainactivity extends Activity {//Declaration EditText Control Object Private EditText et    _content;//declaration TextView Control Object Private TextView tv_content;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        Gets the control object et_content = (EditText) Findviewbyid (r.id.et_content);    Tv_content = (TextView) Findviewbyid (r.id.tv_content);    } public void ClickView (View v) {//Get EditText input text value String content = Et_content.gettext (). toString (); Determines whether an empty if (content) | | textutils.isempty.    Equals (content)) {Toast.maketext (this, "content cannot be empty", 1). Show ();    Return }//Convert to phonetic String value = Hanzitopinyin.getiNstance (). transliterate (content);    Displays the content to the TextView control Tv_content.settext (value); }}


7. Project manifest file

<?xml    Version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.pinyin" android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:m insdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" an        droid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest> 

Note that the above code is in the android4.4.2 system testing, but will be back to some bugs, such as heavy Chong heavy Zhong to such a polyphone and other processing problems.

19_ the conversion of Chinese characters and stitching using the Hanzitopinyin tools provided by Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.