DEV Community

SameX
SameX

Posted on

实现多子类型输入法:如何在 HarmonyOS中加载不同的输入模式

本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前API12)的技术细节,基于实际开发实践进行总结。
主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。
本文为原创内容,任何形式的转载必须注明出处及原作者。

在当今全球化的时代,支持多语言输入是输入法应用的基本功能之一。HarmonyOS 的 IME Kit 为开发者提供了灵活的子类型机制,可以轻松实现多语言键盘布局、模式切换等功能,为用户带来更加便捷和个性化的输入体验。本文将深入探讨 IME Kit 的子类型功能,并介绍如何配置和切换子类型,以及如何监听子类型变化事件,帮助你构建功能丰富的多子类型输入法应用。

输入法子类型简介与应用场景

输入法子类型允许输入法应用展现不同的输入模式或语言,用户可以根据需要在不同模式和语言之间切换。例如,输入法的中英文键盘、手写板、语音输入等都属于输入法的子类型。
子类型的应用场景

  • 多语言支持: 开发者可以为不同的语言创建不同的子类型,例如中文键盘、英文键盘、日文键盘等,用户可以根据需要切换不同的语言键盘。
  • 模式切换: 开发者可以为不同的输入模式创建不同的子类型,例如符号键盘、表情符号键盘、语音输入键盘等,用户可以根据需要切换不同的输入模式。
  • 个性化定制: 开发者可以为不同的用户群体创建不同的子类型,例如儿童键盘、老人键盘、游戏键盘等,满足不同用户群体的个性化需求。 ### 子类型配置与加载 1. InputMethodSubtype 子类型的定义与配置 每个子类型都需要定义一个 InputMethodSubtype 对象,该对象包含以下属性:
  • id: 子类型的唯一标识符。
  • label: 子类型的标签,用于在输入法切换列表中显示。
  • icon: 子类型的图标,用于在输入法切换列表中显示。
  • locale: 子类型支持的语言区域。
  • mode: 子类型的输入模式,例如 "lower"、"upper"、"symbols" 等。 2. 在 module.json5 中注册子类型信息module.json5 配置文件中添加 metadata 字段,name 设置为 ohos_extension.input_method,并在 resource 字段中指定子类型配置文件的路径。 示例代码
"module": {
    // ...
    "extensionAbilities": [
        "description": "InputMethodExtDemo",
        "icon": "$media:icon",
        "name": "InputMethodExtAbility",
        "srcEntry": "./ets/InputMethodExtensionAbility/InputMethodService.ts",
        "type": "inputMethod",
        "exported": true,
        "metadata": [
            "name": "ohos.extension.input_method",
            "resource": "$profile:input_method_config"
        ]
    ]
}
Enter fullscreen mode Exit fullscreen mode

3. 子类型配置文件格式
子类型配置文件使用 JSON 格式,包含 subtypes 数组,数组中的每个元素都是一个 InputMethodSubtype 对象。
示例代码

{
    "subtypes": [
        {
            "icon": "$media:icon",
            "id": "InputMethodExtAbility",
            "label": "$string:english",
            "locale": "en-US",
            "mode": "lower"
        },
        {
            "icon": "$media:icon",
            "id": "InputMethodExtAbility1",
            "label": "$string:chinese",
            "locale": "zh-CN",
            "mode": "lower"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

子类型切换与事件监听

1. 使用 switchCurrentInputMethodSubtype 切换子类型
IME Kit 提供了 switchCurrentInputMethodSubtype() 方法用于切换当前输入法应用的子类型。
示例代码

import { InputMethodSubtype, inputMethod } from '@kit.IMEKit';
export class KeyboardController {
    async switchCurrentInputMethodSubtype() {
        let subTypes = await inputMethod.getSetting().listCurrentInputMethodSubtype();  // 获取当前输入法的所有子类型
        let currentSubType = inputMethod.getCurrentInputMethodSubtype(); // 获取当前输入法当前的子类型
        for(let i=0;i<subTypes.length;i++) {
            if(subTypes[i].id != currentSubType.id) { // 判断不是当前的子类型时切换,实际开发中可以根据需要填固定子类型
                await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. setSubtype 事件监听器的实现
开发者可以使用 inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { ... }) 方法监听子类型变化事件,并根据子类型信息动态切换键盘布局。
示例代码

import { InputMethodSubtype, inputmethodEngine } from '@kit.IMEKit';
let panel: inputmethodEngine.Panel;
let inputMethodAbility: inputmethodEngine.InputMethodAbility = inputmethodEngine.getInputMethodAbility();
inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => {
    let subType = inputMethodSubtype; // 保存当前输入法子类型, 此处也可以改变状态变量的值,布局中判断状态变量,不同的子类型显示不同的布局控件
    if (inputMethodSubtype.id == 'InputMethodExtAbility') { // 根据不同的子类型,可以加载不同的软键盘界面
        panel.setUiContent('pages/Index');
    }
    if (inputMethodSubtype.id == 'InputMethodExtAbility1') { // 根据不同的子类型,可以加载不同的软键盘界面
        panel.setUiContent('pages/Index1');
    }
});
Enter fullscreen mode Exit fullscreen mode

总结

IME Kit 的子类型功能为我们开发者提供了强大的多语言支持和输入模式切换能力,可以帮助开发者构建功能丰富的输入法应用。本文介绍了子类型的概念、配置、切换和事件监听等操作,帮助你实现多子类型输入法应用。
下一步的思考

  • 创建一个支持多种语言的输入法应用,例如中文、英文、日文等。
  • 添加更多的输入模式,例如符号键盘、表情符号键盘、语音输入等。
  • 优化子类型切换逻辑,例如根据用户习惯自动切换子类型。

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay