_利用android提供的HanziToPinyin工具类实现汉字与拼接的转换
来源:互联网 发布:c语言猜数字游戏 编辑:程序博客网 时间:2024/05/17 02:56
汉字转换成拼接的效果有很多实现方式,例如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向这样的多音字等处理上有一些问题.
- 19_利用android提供的HanziToPinyin工具类实现汉字与拼接的转换
- _利用android提供的HanziToPinyin工具类实现汉字与拼接的转换
- 汉字转拼音android实现(HanziToPinyin类)
- 汉字转拼音android实现(HanziToPinyin类)
- 汉字转拼音android实现(HanziToPinyin类)
- 汉字转拼音android实现(HanziToPinyin类)
- Android汉字转拼音HanziToPinyin
- Android汉字转拼音HanziToPinyin
- pinyin4j汉字转换拼音的工具类
- 利用Java的反射机制实现Bean实体与Map自动转换的工具类
- 简体汉字与繁体汉字的转换
- JavaEE_Mybatis_SpringMVC_Mybatis_lesson10__利用foreach标签_拼接SQL串,实现IN(X,Y,Z,...的形式)
- 汉字转换为拼音字头的工具类与原理
- android提供的算法工具类
- android 工具类: 将汉字转换
- 利用android源码实现获取汉字的拼音
- 利用android源码实现获取汉字的拼音
- Unicode与汉字的转换
- 看着脚下默默前行
- 3d数学基础-RotationMatrix-惯性坐标系到物体坐标系,物体坐标系到惯性坐标系-用C++代码实现
- 重温ASP.NET内置对象之Request
- prototype和__proto__
- 关于百度搜索之后后面的%23%%%%是怎么出来的
- _利用android提供的HanziToPinyin工具类实现汉字与拼接的转换
- memcache同步session的方法
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Git的进阶使用
- 自定义类似优酷首页的BannerView幻灯片展示
- Two Sum
- I,P,B帧和PTS,DTS的关系
- Who I am