首页 > 其他分享 >Android实战--汉字转全拼

Android实战--汉字转全拼

时间:2022-11-11 11:32:53浏览次数:85  
标签:layout String 全拼 -- id hanzi import Android android


上一节介绍了天气预报小DEMO的制作,有些人就觉得打拼音不符合用户的习惯,怎么改成打汉字并且可以实现功能呢?这里就要用到汉字转全拼,这时我们就要引入jar包:

将jar包解压后导入我们的工程:(导入方法自行百度)

Android实战--汉字转全拼_api

然后我们编写我们的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="南京" />

<Button
android:id="@+id/transfomer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="转换" />

<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />

</LinearLayout>

编写工具类:

package com.lzg.hanzitopinyin;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class HanZiToPinYin {
/**
* 返回一个字的拼音
* @param hanzi
* @return
*/
public static String toPinYin(char hanzi){
HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
hanyuPinyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//没有声调
hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
String[] pinyinArray=null;
try {
//是否在汉字范围内
if(hanzi>=0x4e00 && hanzi<=0x9fa5){
//pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
//将汉字返回
return pinyinArray[0];
}
}

编写activity:

package com.lzg.hanzitopinyin;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends Activity {
/** Called when the activity is first created. */
EditText editText;
TextView tView;
Button trbButton;
String pinyin;
String hanziString;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.edit);
tView = (TextView) findViewById(R.id.show);
trbButton = (Button) findViewById(R.id.transfomer);
trbButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
hanziString = editText.getText().toString();
pinyin = getPinyin(hanziString);
tView.setText("拼音:" + pinyin + "\n" + "汉字:" + hanziString);

}
});

}

private String getPinyin(String hanzi) {
String pinyin = "";
for (int i = 0; i < hanzi.length(); i++) {
pinyin += HanZiToPinYin.toPinYin(hanzi.charAt(i));

}
return pinyin;
}
}

 

运行实例如下:

Android实战--汉字转全拼_xml_02

最后:

我的应用:                      ​​http://openbox.mobilem.360.cn/index/d/sid/2966005​

                                       ​​http://android.myapp.com/myapp/detail.htm?apkName=com.yayun.gitlearning​

欢迎下载,有问题多交流,讨论谢谢!

 

标签:layout,String,全拼,--,id,hanzi,import,Android,android
From: https://blog.51cto.com/u_15866446/5843768

相关文章

  • PHP判断内网/外网IP
        工作中用到PHP来判断内外网IP,查找资料偶然发现已有现成的实现函数,cool!filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_R......
  • 促进整合经营管理,贝锐蒲公英打造连锁门店收银系统远程互联方案
    伴随市民生活水平的日益提高,如今大家对于便捷性消费购物的需求也越来越大。在众多国外品牌连锁便利店入驻的同时,更多优秀的国内连锁加盟品牌也渐渐地开始崭露头角。由于连......
  • invalid PID number
     在Mac上用brew安装Nginx,然后修改Nginx配置文件,再重启时报出如下错误:nginx:[error]invalidPIDnumber""in"/usr/local/var/run/nginx/nginx.pid"解决办法:$sudong......
  • php 获取文件mime类型的方法
     php获取文件mime类型的方法 1.使用mime_content_type方法stringmime_content_type(string$filename)ReturnstheMIMEcontenttypeforafileasdeterminedb......
  • 刷屏洗脑的“吗咿呀嘿”,到底是个啥?
    “蚂蚁呀嘿,蚂蚁牙呼,蚂蚁牙呼呼呼~”好洗脑的节奏啊~相信不少朋友跟「架构精进之路」作者一样,这个元宵节周末就被这首“蚂蚁呀嘿”刷屏、洗脑了。这个BGM原本出自新加坡歌手......
  • 获取系统所有进程
    //进程获取publicvoidmpid()throwsIOException{Stringname=ManagementFactory.getRuntimeMXBean().getName();//System.out.println(name);Strin......
  • leetcode(35)位运算系列题目
    不需要额外空间的方法,就往位运算上想136.只出现一次的数字异或运算的性质:1.交换律:a^b^c<=>a^c^b2.任何数于0异或为任何数0^n=>n3.相同的数异或为0:......
  • 微信DAT文件解密(dat转图像)
    微信电脑版现在已经是日常工作生活必不可少的工具,有时候删除了聊天记录或者被系统清理软件清理了,但还想查看曾经的微信聊天图片。这个时候辛辛苦苦找到了文件,却发现无法查......
  • 记一次 压测调优
    背景:同步生产环境的MySQL、Redis到腾讯云,作为test2压测环境 【具体优化点】一、数据库连接池修改petem-booking服务最大链接池数量maximum-pool-size、minimum......
  • 房产信息管理系统--重置密码
    重置密码:管理员可以修改房产经纪人的个人密码,先按照房产经纪人工号查询,显示出基本信息后,点击密码重置,将房产经纪人密码统一修改为“123456”。(2分)此功能涉及的就是查和改......