_利用android提供的HanziToPinyin工具类实现汉字与拼接的转换

来源:互联网 发布:c语言猜数字游戏 编辑:程序博客网 时间:2024/05/17 02:56

    汉字转换成拼接的效果有很多实现方式,例如pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换。拼音输出格式可以定制(后面的博客会详细介绍使用方法)。但是在android的系统应用联系人中也给我们实现了汉字与拼接转换的方式,接下来我们简单介绍一下android中汉字与拼音的转换。

   1.首先到https://github.com/android网站上去下载android提供联系人接口的源码android/platform_packages_providers_contactsprovider

   

点击 DownLoad ZIP 即可下载

  2.找到platform_packages_providers_contactsprovider-master\src\com\android\providers\contacts 此目录中的HanziToPinyin.java 拷贝到项目中,你会发现此类依赖于底层中的Transliterator.java这个类 所以也要下载此类(源码位于:source\libcore\luni\src\main\java\libcore\icu),下面给出此类的源码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2013 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16. package libcore.icu;  
  17. /** 
  18.  * Exposes icu4c's Transliterator. 
  19.  */  
  20. public final class Transliterator {  
  21.   private long peer;  
  22.   /** 
  23.    * Creates a new Transliterator for the given id. 
  24.    */  
  25.   public Transliterator(String id) {  
  26.     peer = create(id);  
  27.   }  
  28.   @Override protected synchronized void finalize() throws Throwable {  
  29.     try {  
  30.       destroy(peer);  
  31.       peer = 0;  
  32.     } finally {  
  33.       super.finalize();  
  34.     }  
  35.   }  
  36.   /** 
  37.    * Returns the ids of all known transliterators. 
  38.    */  
  39.   public static native String[] getAvailableIDs();  
  40.   /** 
  41.    * Transliterates the specified string. 
  42.    */  
  43.   public String transliterate(String s) {  
  44.     return transliterate(peer, s);  
  45.   }  
  46.   private static native long create(String id);  
  47.   private static native void destroy(long peer);  
  48.   private static native String transliterate(long peer, String s);  
  49. }  


3.下面通过一个案例 说明使用

    3.1效果如下

       

     备注1.在输入框中输入汉字,点击转换按钮就可以看到效果

 

4.项目的目录结构

    

    此目录 可以看到com.example.util及libcore.icu包中的类都是引入android系统的代码,接下来我们具体看一下MainActivity.java的实现.

5.首先看一下MainActivity对应的界面的布局文件

   

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context="${relativePackage}.${activityClass}" >  
  7.   
  8.     <EditText  
  9.         android:id="@+id/et_content"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/tv_content"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"   
  17.         android:layout_marginTop="20dp"  
  18.         android:layout_marginBottom="20dp"  
  19.         android:text="@string/hello_world"/>  
  20.   
  21.     <Button  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:onClick="clickView"  
  25.         android:text="转换" />  
  26.   
  27. </LinearLayout>  


 

6.MainActivity中的实现

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.pinyin;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.TextUtils;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10.   
  11. import com.example.util.HanziToPinyin;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     //声明EditText控件对象  
  16.     private EditText et_content;  
  17.     //声明TextView控件对象  
  18.     private TextView tv_content;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         //获取控件对象  
  25.         et_content = (EditText) findViewById(R.id.et_content);  
  26.         tv_content = (TextView) findViewById(R.id.tv_content);  
  27.     }  
  28.       
  29.     public void clickView(View v){  
  30.         //获取EditText输入的文本值  
  31.         String content = et_content.getText().toString();  
  32.         //判断是否为空  
  33.         if(TextUtils.isEmpty(content)||"".equals(content)){  
  34.             Toast.makeText(this, "内容不能为空", 1).show();  
  35.             return;  
  36.         }  
  37.           
  38.         //转换成拼音  
  39.         String value = HanziToPinyin.getInstance().transliterate(content);  
  40.         //把内容显示到TextView控件上  
  41.         tv_content.setText(value);  
  42.     }  
  43. }  


7.项目清单文件

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.pinyin"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="19" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name=".MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  

 

备注说明以上代码是在android4.4.2系统中测试,但是会返现一些bug,比如 重chong 重 zhong向这样的多音字等处理上有一些问题.

        

  

 

  

0 0
原创粉丝点击