19 _ use the HanziToPinyin tool class provided by android to convert Chinese characters and splice them.

Source: Internet
Author: User

19 _ use the HanziToPinyin tool class provided by android to convert Chinese characters and splice them.

There are many implementation methods for converting Chinese characters into concatenated characters. For example, 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 the usage ). However, in the android system application contacts, we also implemented the conversion of Chinese characters and splicing methods. Next we will briefly introduce the conversion of Chinese characters and Pinyin in android.

1. First go to https://github.com/android to download the source code of android/Platform_packages_providers_contactsprovider

Click DownLoad ZIP to DownLoad

2. locate HanziToPinyin the platform_packages_providers_contactsprovider-master \ src \ com \ android \ providers \ contacts directory. if java is copied to the project, you will find that this class depends on the Transliterator in the underlying layer. the java class also needs to be downloaded (the source code is located at: source \ libcore \ luni \ src \ main \ java \ libcore \ icu). The source code for this class is as follows:

/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "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. The following example shows how to use

3.1 effect:

Note 1. Enter Chinese characters in the input box and click the convert button to see the effect.

 

4. directory structure of the project

In this directory, we can see that the classes in the com. example. util and libcore. icu packages introduce the android code. Next, let's take a look at the implementation of MainActivity. java.

5. First, let's take a look at the layout file of the MainActivity 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>


 

6. MainActivity implementation

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 {// declare the EditText control object private EditText et_content; // declare the TextView control object private TextView TV _content; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the control object et_content = (EditText) findViewById (R. id. et_content); TV _content = (TextView) findViewById (R. id. TV _content);} public void clickView (View v) {// obtain the EditText value String content = et_content.getText (). toString (); // determines whether it is null if (TextUtils. isEmpty (content) | "". equals (content) {Toast. makeText (this, "content cannot be blank", 1 ). show (); return;} // converts the String value to HanziToPinyin. getInstance (). transliterate (content); // display the content to the TextView control. TV _content.setText (value );}}


7. project list 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:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android: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 tested in the android4.4.2 system, but some bugs will be returned. For example, there are some problems with the processing of heavy chong and heavy zhong to such multi-Tone Words.

 

Related Article

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.