【JAVA UI】HarmonyOS 如何使用TinyPinyin类库

 参考资料

前言: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);
            }
        });
    }
}

 

运行效果

cke_19722.png

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

posted @ 2022-08-12 09:31  华为开发者论坛  阅读(88)  评论(0编辑  收藏  举报