参考资料

前言:TinyPinYin是一个适用于Java和Android、HarmonyOS的快速,低内存的汉字转拼音库。码云地址TinyPinYin,其使用方法已在API讲解中有详细介绍,本文的主要目的主要是对容易造成疑问的地方进行补充说明

代码实现

1、集成类库

在项目级bulid.gradle添加如下代码

buildscript {
repositories {
.......
mavenCentral()
}
.....
}

在应用级添加如下代码

dependencies {
.......
implementation 'io.openharmony.tpc.thirdlib:TinyPinyin-Library:1.0.4'
}

2、api讲解

汉字转化拼音

String tv = Pinyin.toPinyin("哈哈", "");

判断是否字符

     String test = "34我23们";
char[] chars = test.toCharArray();
for (char aChar : chars) { LogUtil.error(TAG, Pinyin.isChinese(aChar) + ""); }

多音字

    Pinyin.init(Pinyin.newConfig().with(new PinyinMapDict() {
@Override
public Map<String, String[]> mapping() {
HashMap<String, String[]> map = new HashMap<String, String[]>();
map.put("中国重庆", new String[]{"ZHONG", "GUO", "CHONG", "QING"});
return map;
}
})); result.setText(Pinyin.toPinyin("中国重庆", ""));

添加分隔符

   String tv =   Pinyin.toPinyin("草原牧羊", "~");

3、xml布局绘画

在xml界面绘画两个“text组件”,其中一个用于现在“汉字转化为拼音”,另外一个Text组件用于实现点击“汉字转化为拼音”的Text组件按钮,转化为拼音的结果用于显示结果,代码如下

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="top"
ohos:orientation="vertical"> <Text
ohos:id="$+id:text_helloworld"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="#ed6262"
ohos:layout_alignment="horizontal_center"
ohos:multiple_lines="true"
ohos:text="汉字转化为拼音"
ohos:text_color="black"
ohos:text_size="25vp"
/>
<Text
ohos:height="match_parent"
ohos:width="match_parent"
ohos:top_margin="10vp"
ohos:text_alignment="top|left"
ohos:text_size="25vp"
ohos:id="$+id:result"/> </DirectionalLayout>

4、java代码实现

java实现代码如下

package com.newdemo.myapplication.slice;

import com.example.library.github.promeg.pinyinhelper.Pinyin;
import com.example.library.github.promeg.pinyinhelper.PinyinMapDict;
import com.newdemo.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
private Text mTextResult;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
mTextResult=findComponentById(ResourceTable.Id_result);
findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
String tv = Pinyin.toPinyin("哈哈", "");
mTextResult.setText("转化的结果:"+tv);
}
});
}
}

运行效果

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

最新文章

  1. 清晰易懂TCP通信原理解析(附demo、简易TCP通信库源码、解决沾包问题等)C#版
  2. touches
  3. zabbix常用术语
  4. 分页进阶--ajax+jquery+struts2
  5. 【leetcode】Plus One (easy)
  6. Python 日期和时间
  7. LA 3027 Corporative Network
  8. 原创: 做一款属于自己风格的音乐播放器 (HTML5的Audio新特性)
  9. 小白日记37:kali渗透测试之Web渗透-手动漏洞挖掘(三)-目录遍历、文件包含
  10. 啊哈,yield
  11. CSS 盒子模型(转)
  12. H5中背景音乐无法自动播放问题
  13. linux cenots7安装mysql
  14. 小试ImageMagik——开发篇
  15. [Swift]LeetCode769. 最多能完成排序的块 | Max Chunks To Make Sorted
  16. Oracle中和mysql中函数的区别
  17. 学习笔记-AngularJs(八)
  18. 【OpenStack】相关概念
  19. 如何在css中设置按钮button中包含图片文字对齐方式
  20. ajax 异步请求

热门文章

  1. css页面样式初始化
  2. 「ARC138E」Decreasing Subsequence(n logn 做法)
  3. [学习笔记] pd_ds黑科技
  4. .NET C#基础(1):相等性与同一性判定 - 似乎有点小缺陷的设计
  5. tf.data(二) —— 并行化 tf.data.Dataset 生成器
  6. 开发工具-MySQL下载地址
  7. Node.js精进(5)——HTTP
  8. grafana整合zabbix
  9. Flink1.13.1源码解析-Application on yarn(一)
  10. 排名前三——python 开源 IDE