1# 自绘编辑框开发指导 (C/C++) 2 3 4## 场景介绍 5 6IME Kit支持开发者使用自绘组件开发自定义编辑框,与输入法应用交互,包括显示、隐藏输入法,接收来自输入法应用的文本编辑操作通知等,本文档介绍开发者如何使用C/C++完成此功能开发。 7 8## 接口说明 9 10详细接口说明请参考[InputMethod接口文档](../reference/apis-ime-kit/_input_method.md)。 11 12## 添加动态链接库 13 14CMakeLists.txt中添加以下lib。 15 16```txt 17libohinputmethod.z.so 18``` 19 20## 引用头文件 21 22```c 23#include <inputmethod/inputmethod_controller_capi.h> 24``` 25 26 27## 绑定输入法 28 29开发者需要在输入框获焦时,通过调用接口[OH_InputMethodController_Attach](../reference/apis-ime-kit/_input_method.md#oh_inputmethodcontroller_attach)绑定输入法,绑定成功后用户可以通过输入法输入文字。 30 311. 创建InputMethod_TextEditorProxy实例,示例代码如下所示: 32 33 ```c 34 // 创建InputMethod_TextEditorProxy实例 35 InputMethod_TextEditorProxy *textEditorProxy = OH_TextEditorProxy_Create(); 36 ``` 37 383. 创建InputMethod_AttachOptions实例,设置绑定输入法时的选项。示例代码如下所示: 39 40 ```c 41 // 创建InputMethod_AttachOptions实例,选项showKeyboard用于指定此次绑定成功后是否显示键盘,此处以目标显示键盘为例 42 bool showKeyboard = true; 43 InputMethod_AttachOptions *options = OH_AttachOptions_Create(showKeyboard); 44 ``` 45 464. 调用OH_InputMethodController_Attach发起绑定输入法服务,调用成功后,可以获取到用于和输入法交互的InputMethod_InputMethodProxy。示例代码如下所示: 47 48 ```c 49 InputMethod_InputMethodProxy *inputMethodProxy = nullptr; 50 // 发起绑定请求 51 if (OH_InputMethodController_Attach(textEditorProxy, options, &inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 52 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "Attach failed!"); 53 } 54 ``` 55 56## 显示/隐藏面板功能 57 58绑定成功后,可以使用获取到的[InputMethod_InputMethodProxy](../reference/apis-ime-kit/_input_method.md#inputmethod_inputmethodproxy)对象向输入法发送消息。示例代码如下所示: 59 60```c 61// 显示键盘 62if (OH_InputMethodProxy_ShowKeyboard(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 63 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "ShowKeyboard failed!"); 64} 65// 隐藏键盘 66if (OH_InputMethodProxy_HideKeyboard(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 67 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "HideKeyboard failed!"); 68} 69// 通知输入框配置信息变化 70if (OH_InputMethodProxy_NotifyConfigurationChange(inputMethodProxy, InputMethod_EnterKeyType::IME_ENTER_KEY_GO, InputMethod_TextInputType::IME_TEXT_INPUT_TYPE_TEXT) != InputMethod_ErrorCode::IME_ERR_OK) { 71 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "NotifyConfigurationChange failed!"); 72} 73``` 74 75## 监听输入法应用的请求/通知 76 771. 需要先实现对输入法应用发送的请求或通知的响应处理函数,示例代码如下所示: 78 79 ```c 80 // 实现InputMethod_TextEditorProxy中的输入法应用事件响应函数 81 void GetTextConfig(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_TextConfig *config) 82 { 83 // 处理输入法发送的获取输入框配置请求 84 } 85 void InsertText(InputMethod_TextEditorProxy *textEditorProxy, const char16_t *text, size_t length) 86 { 87 // 处理输入法发送的插入文本请求 88 } 89 void DeleteForward(InputMethod_TextEditorProxy *textEditorProxy, int32_t length) 90 { 91 // 处理输入法发送的删除文本请求 92 } 93 // ...... 94 ``` 95 962. 将实现后的响应函数,设置到[InputMethod_TextEditorProxy](../reference/apis-ime-kit/_input_method.md#inputmethod_texteditorproxy)中,再通过绑定输入法时调用的[OH_InputMethodController_Attach](../reference/apis-ime-kit/_input_method.md#oh_inputmethodcontroller_attach)将其设置到输入法框架中,完成监听注册。示例代码如下所示 97 98 ```c 99 // 将实现好的响应处理函数设置到InputMethod_TextEditorProxy中 100 OH_TextEditorProxy_SetGetTextConfigFunc(textEditorProxy, GetTextConfig); 101 OH_TextEditorProxy_SetInsertTextFunc(textEditorProxy, InsertText); 102 OH_TextEditorProxy_SetDeleteForwardFunc(textEditorProxy, DeleteForward); 103 ``` 104 105## 解绑输入法 106 107当编辑框失焦,需要结束使用输入法,通过接口[OH_InputMethodController_Detach](../reference/apis-ime-kit/_input_method.md#oh_inputmethodcontroller_detach)与输入法框架解绑。 108 109```c 110// 发起解绑请求 111if (OH_InputMethodController_Detach(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 112 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "Detach failed!"); 113} 114inputMethodProxy = nullptr; 115OH_TextEditorProxy_Destroy(textEditorProxy); 116textEditorProxy = nullptr; 117OH_AttachOptions_Destroy(options); 118options = nullptr; 119``` 120 121