OpenHarmony 实战开发 —— 自绘编辑框开发指南 (C/C++)

场景介绍

IME Kit支持开发者使用自绘组件开发自定义编辑框,与输入法应用交互,包括显示、隐藏输入法,接收来自输入法应用的文本编辑操作通知等,本文档介绍开发者如何使用C/C++完成此功能开发。

接口说明

详细接口说明请参考 InputMethod接口文档 。

添加动态链接库

CMakeLists.txt中添加以下lib。

txt 复制代码
libohinputmethod.z.so

引用头文件

c 复制代码
#include <inputmethod/inputmethod_controller_capi.h>

绑定输入法

开发者需要在输入框获焦时,通过调用接口OH_InputMethodController_Attach 绑定输入法,绑定成功后用户可以通过输入法输入文字。

  1. 创建InputMethod_TextEditorProxy实例,示例代码如下所示:
c 复制代码
    // 创建InputMethod_TextEditorProxy实例
    InputMethod_TextEditorProxy *textEditorProxy = OH_TextEditorProxy_Create();
  1. 创建InputMethod_AttachOptions实例,设置绑定输入法时的选项。示例代码如下所示:
c 复制代码
    // 创建InputMethod_AttachOptions实例,选项showKeyboard用于指定此次绑定成功后是否显示键盘,此处以目标显示键盘为例
    bool showKeyboard = true;
    InputMethod_AttachOptions *options = OH_AttachOptions_Create(showKeyboard);
  1. 调用OH_InputMethodController_Attach发起绑定输入法服务,调用成功后,可以获取到用于和输入法交互的InputMethod_InputMethodProxy。示例代码如下所示:
c 复制代码
    InputMethod_InputMethodProxy *inputMethodProxy = nullptr;
    // 发起绑定请求
    if (OH_InputMethodController_Attach(textEditorProxy, options, &inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "Attach failed!");
    }

DD一下: 欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。

erlang 复制代码
`欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

显示/隐藏面板功能

绑定成功后,可以使用获取到的InputMethod_InputMethodProxy 对象向输入法发送消息。示例代码如下所示:

c 复制代码
// 显示键盘
if (OH_InputMethodProxy_ShowKeyboard(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "ShowKeyboard failed!");
}
// 隐藏键盘
if (OH_InputMethodProxy_HideKeyboard(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "HideKeyboard failed!");
}
// 通知输入框配置信息变化
if (OH_InputMethodProxy_NotifyConfigurationChange(inputMethodProxy, InputMethod_EnterKeyType::IME_ENTER_KEY_GO, InputMethod_TextInputType::IME_TEXT_INPUT_TYPE_TEXT) != InputMethod_ErrorCode::IME_ERR_OK) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "NotifyConfigurationChange failed!");
}

监听输入法应用的请求/通知

  1. 需要先实现对输入法应用发送的请求或通知的响应处理函数,示例代码如下所示:
c 复制代码
    // 实现InputMethod_TextEditorProxy中的输入法应用事件响应函数
    void GetTextConfig(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_TextConfig *config)
    {
        // 处理输入法发送的获取输入框配置请求
    }
    void InsertText(InputMethod_TextEditorProxy *textEditorProxy, const char16_t *text, size_t length)
    {
        // 处理输入法发送的插入文本请求
    }
    void DeleteForward(InputMethod_TextEditorProxy *textEditorProxy, int32_t length)
    {
        // 处理输入法发送的删除文本请求
    }
    // ......
  1. 将实现后的响应函数,设置到InputMethod_TextEditorProxy 中,再通过绑定输入法时调用的 OH_InputMethodController_Attach 将其设置到输入法框架中,完成监听注册。示例代码如下所示
c 复制代码
    // 将实现好的响应处理函数设置到InputMethod_TextEditorProxy中
    OH_TextEditorProxy_SetGetTextConfigFunc(textEditorProxy, GetTextConfig);
    OH_TextEditorProxy_SetInsertTextFunc(textEditorProxy, InsertText);
    OH_TextEditorProxy_SetDeleteForwardFunc(textEditorProxy, DeleteForward);

解绑输入法

当编辑框失焦,需要结束使用输入法,通过接口OH_InputMethodController_Detach 与输入法框架解绑。

c 复制代码
// 发起解绑请求
if (OH_InputMethodController_Detach(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "Detach failed!");
}
inputMethodProxy = nullptr;
OH_TextEditorProxy_Destroy(textEditorProxy);
textEditorProxy = nullptr;
OH_AttachOptions_Destroy(options);
options = nullptr;
相关推荐
半路下车8 分钟前
【Harmony OS 5】HarmonyOS应用测试指南
设计模式·harmonyos
移动端开发者11 分钟前
鸿蒙Next自定义双滑块滑动条实现方案
harmonyos
SunFlower0113 分钟前
Harmony-websocket(一)
harmonyos
泽020221 分钟前
C++之STL--list
开发语言·c++·list
颜颜yan_29 分钟前
深入解析HarmonyOS5 UIAbility组件:从核心架构到实战应用
架构·harmonyos·鸿蒙·鸿蒙系统
程序员小刘37 分钟前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
半路下车1 小时前
【Harmony OS 5】HarmonyOS应用测试进阶
测试·harmonyos
Dovis(誓平步青云)2 小时前
探索C++标准模板库(STL):String接口的底层实现(下篇)
开发语言·c++·stl·string
KyollBM3 小时前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法