汉字转换成拼接的效果有很多实现方式,例如pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换。拼音输出格式可以定制(后面的博客会详细介绍使用方法)。但是在android的系统应用联系人中也给我们实现了汉字与拼接转换的方式,接下来我们简单介绍一下android中汉字与拼音的转换。
1.首先到https://github.com/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对应的界面的布局文件
- <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="转换" />
- </LinearLayout>
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"?>
- <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>
备注说明以上代码是在android4.4.2系统中测试,但是会返现一些bug,比如 重chong 重 zhong向这样的多音字等处理上有一些问题.