汉字转换成拼接的效果有很多实现方式,例如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),下面给出此类的源码如下:

/* * 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.下面通过一个案例 说明使用

    3.1效果如下

       

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

 

4.项目的目录结构

   

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

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

   

            


 

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 {//声明EditText控件对象private EditText et_content;//声明TextView控件对象private TextView tv_content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                //获取控件对象        et_content = (EditText) findViewById(R.id.et_content);        tv_content = (TextView) findViewById(R.id.tv_content);    }        public void clickView(View v){    //获取EditText输入的文本值    String content = et_content.getText().toString();    //判断是否为空    if(TextUtils.isEmpty(content)||"".equals(content)){    Toast.makeText(this, "内容不能为空", 1).show();    return;    }        //转换成拼音    String value = HanziToPinyin.getInstance().transliterate(content);    //把内容显示到TextView控件上    tv_content.setText(value);    }}


7.项目清单文件

 

<?xml version="1.0" encoding="utf-8"?>                                                                                    

 

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

        

  

 

  

 

更多相关文章

  1. android学到啥时候就高级了啊
  2. Android中控件的继承 通用行为和属性
  3. android 中Xml里面的id重名问题
  4. 用c/c++混合编程方式为ios/android实现一个自绘日期选择控件(一)
  5. xml的用法
  6. android几种布局
  7. Android中android:visibility的3中属性的剖析
  8. Android(安卓)layout xml总结
  9. Android:为控件绑定监听器

随机推荐

  1. Android(安卓)GPS获得经纬度并得到该坐标
  2. Android(安卓)http POST
  3. Android(安卓)- 保持在底部的按钮栏,上面
  4. android EditText 监听
  5. android如何获取后台正在运行的service
  6. android > 获取 通讯录 信息
  7. checkbox style
  8. Android(安卓)- 解决onSaveInstanceState
  9. android使用全局变量的方法
  10. ANDROID ONTOUCHEVENT, ONCLICK及ONLONGC