未验证 提交 ab7d36b0 编写于 作者: O openharmony_ci 提交者: Gitee

!22464 同步22380修改,输入法应用开发指南中需要更新示例代码

Merge pull request !22464 from ningning/cherry-pick-1692323348
...@@ -56,14 +56,12 @@ ...@@ -56,14 +56,12 @@
```ts ```ts
import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility'; import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility';
import { KeyboardController } from './model/KeyboardController' import keyboardController from './model/KeyboardController'
export default class InputDemoService extends InputMethodExtensionAbility { export default class InputDemoService extends InputMethodExtensionAbility {
private keyboardController: KeyboardController;
onCreate(want) { onCreate(want) {
this.keyboardController = new KeyboardController(this.context); keyboardController.onCreate(this.context); // 初始化窗口并注册对输入法框架的事件监听
this.keyboardController.onCreate(); // 初始化窗口并注册对输入法框架的事件监听
} }
onDestroy() { onDestroy() {
...@@ -72,32 +70,29 @@ ...@@ -72,32 +70,29 @@
} }
} }
``` ```
2. KeyboardController.ts文件。 2. KeyboardController.ts文件。
```ts ```ts
import common from '@ohos.app.ability.common';
import inputMethodEngine from '@ohos.inputMethodEngine'; import inputMethodEngine from '@ohos.inputMethodEngine';
import display from '@ohos.display'; import display from '@ohos.display';
import windowManager from '@ohos.window';
// 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口 // 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口
globalThis.inputAbility = inputMethodEngine.getInputMethodAbility(); const inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
export class KeyboardController { export class KeyboardController {
mContext; // 保存InputMethodExtensionAbility中的context属性 private mContext: common.ExtensionContext | undefined = undefined; // 保存InputMethodExtensionAbility中的context属性
WINDOW_TYPE_INPUT_METHOD_FLOAT = 2105; // 定义窗口类型,2105代表输入法窗口类型,用于创建输入法应用窗口 private panel: inputMethodEngine.Panel | undefined = undefined;
windowName = 'inputApp'; private textInputClient: inputMethodEngine.InputClient | undefined = undefined;
private windowHeight: number = 0; private keyboardController: inputMethodEngine.KeyboardController | undefined = undefined;
private windowWidth: number = 0;
private nonBarPosition: number = 0;
private isWindowShowing: boolean = false;
constructor(context) { constructor() {
this.mContext = context;
} }
public onCreate(): void public onCreate(context: common.ExtensionContext): void
{ {
this.mContext = context;
this.initWindow(); // 初始化窗口 this.initWindow(); // 初始化窗口
this.registerListener(); // 注册对输入法框架的事件监听 this.registerListener(); // 注册对输入法框架的事件监听
} }
...@@ -105,80 +100,88 @@ ...@@ -105,80 +100,88 @@
public onDestroy(): void // 应用生命周期销毁 public onDestroy(): void // 应用生命周期销毁
{ {
this.unRegisterListener(); // 去注册事件监听 this.unRegisterListener(); // 去注册事件监听
let win = windowManager.findWindow(this.windowName); if(this.panel) { // 销毁窗口
win.destroyWindow(); // 销毁窗口 this.panel.hide();
inputMethodAbility.destroyPanel(this.panel);
}
}
public insertText(text: string): void {
if(this.textInputClient) {
this.textInputClient.insertText(text);
}
}
public deleteForward(length: number): void {
if(this.textInputClient) {
this.textInputClient.deleteForward(length);
}
} }
private initWindow(): void // 初始化窗口 private initWindow(): void // 初始化窗口
{ {
if(this.mContext === undefined) {
return;
}
let dis = display.getDefaultDisplaySync(); let dis = display.getDefaultDisplaySync();
let dWidth = dis.width; let dWidth = dis.width;
let dHeight = dis.height; let dHeight = dis.height;
let keyHeightRate = 0.47; let keyHeightRate = 0.47;
let keyHeight = dHeight * keyHeightRate; let keyHeight = dHeight * keyHeightRate;
this.windowWidth = dWidth; let nonBarPosition = dHeight - keyHeight;
this.windowHeight = keyHeight; let panelInfo: inputMethodEngine.PanelInfo = {
this.nonBarPosition = dHeight - keyHeight; type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
flag: inputMethodEngine.PanelFlag.FLG_FLOATING
let config = { };
name: this.windowName, inputMethodAbility.createPanel(this.mContext, panelInfo).then(async (inputPanel: inputMethodEngine.Panel) => {
windowType: this.WINDOW_TYPE_INPUT_METHOD_FLOAT, this.panel = inputPanel;
ctx: this.mContext if(this.panel) {
} await this.panel.resize(dWidth, keyHeight);
windowManager.createWindow(config).then((win) => { // 根据窗口类型创建窗口 await this.panel.mobeTo(0, nonBarPosition);
win.resize(dWidth, keyHeight).then(() => { await this.panel.setUiContent('inputmethodextability/pages/Index');
win.moveWindowTo(0, this.nonBarPosition).then(() => { }
win.setUIContent('pages/InputMethodExtAbility/Index').then(() => {
});
});
});
}); });
} }
private registerListener(): void private registerListener(): void
{ {
this.registerInputListener(); // 注册对输入法框架服务的监听 this.registerInputListener(); // 注册对输入法框架服务的监听
globalThis.inputAbility.on('keyboardShow', () => { // 注册显示键盘事件监听
if (this.isWindowShowing) {
return;
}
this.isWindowShowing = true;
this.showHighWindow(); // 显示窗口
});
... ...
// 注册隐藏键盘事件监听等 // 注册隐藏键盘事件监听等
} }
private registerInputListener() { // 注册对输入法框架服务的开启及停止事件监听 private registerInputListener(): void { // 注册对输入法框架服务的开启及停止事件监听
globalThis.inputAbility.on('inputStart', (kbController, textInputClient) => { inputMethodAbility.on('inputStart', (kbController, textInputClient) => {
globalThis.textInputClient = textInputClient; // 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口 this.textInputClient = textInputClient; // 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口
globalThis.keyboardController = kbController; this.boardController = kbController;
}) })
globalThis.inputAbility.on('inputStop', (imeId) => { globalThis.inputAbility.on('inputStop', () => {
if (imeId == "包名/Ability名") { this.onDestroy(); // 销毁KeyboardController
this.mContext.destroy(); // 销毁InputMethodExtensionAbility服务
}
}); });
} }
private unRegisterListener(): void private unRegisterListener(): void
{ {
globalThis.inputAbility.off('inputStart'); inputMethodAbility.off('inputStart');
globalThis.inputAbility.off('inputStop', () => {}); inputMethodAbility.off('inputStop', () => {});
globalThis.inputAbility.off('keyboardShow');
} }
private showHighWindow() { private async showHighWindow(): Promise<void> {
let win = windowManager.findWindow(this.windowName) try {
win.resize(this.windowWidth, this.windowHeight).then(() => { if(this.panel) {
win.moveWindowTo(0, this.nonBarPosition).then(() => { await this.panel.resize(this.windowWidth, this.windowHeight);
win.showWindow().then(() => { await this.panel.moveTo(0, this.nonBarPosition);
this.isWindowShowing = false; await this.panel.show();
}) }
}) } catch (e) {
}) console.log('err occur: ' + JSON.stringify(e));
}
} }
} }
const keyboardController = new KeyboardController();
export default keyboardController;
``` ```
3. KeyboardKeyData.ts文件。 3. KeyboardKeyData.ts文件。
...@@ -231,7 +234,8 @@ ...@@ -231,7 +234,8 @@
同时在resources/base/profile/main_pages.json文件的src字段中添加此文件路径。 同时在resources/base/profile/main_pages.json文件的src字段中添加此文件路径。
```ets ```ets
import { numberSourceListData, sourceListType } from './keyboardKeyData' import { numberSourceListData, sourceListType } from './keyboardKeyData';
import keyboardController from '../model/KeyboardController';
@Component @Component
struct keyItem { struct keyItem {
...@@ -250,10 +254,8 @@ ...@@ -250,10 +254,8 @@
.borderRadius(6) .borderRadius(6)
.width("8%") .width("8%")
.height("65%") .height("65%")
.onTouch((event: TouchEvent) => { .onClick(() => {
if (event.type === TouchType.Down) { keyboardController.insertText(this.keyValue.content);
globalThis.textInputClient.insertText(this.keyValue.content);
}
}) })
} }
} }
...@@ -274,10 +276,8 @@ ...@@ -274,10 +276,8 @@
.backgroundColor(this.keyBgc) .backgroundColor(this.keyBgc)
.width("13%") .width("13%")
.borderRadius(6) .borderRadius(6)
.onTouch((event: TouchEvent) => { .onClick(() => {
if (event.type === TouchType.Down) { keyboardController.deleteForward(1);
globalThis.textInputClient.deleteForward(1);
}
}) })
} }
} }
...@@ -338,7 +338,7 @@ ...@@ -338,7 +338,7 @@
} }
} }
``` ```
5. 在工程Module对应的[module.json5配置文件](../quick-start/module-configuration-file.md)中注册InputMethodExtensionAbility,type标签需要设置为“inputMethod”,srcEntry标签表示当前InputMethodExtensionAbility组件所对应的代码路径。 5. 在工程Module对应的[module.json5配置文件](../quick-start/module-configuration-file.md)中注册InputMethodExtensionAbility,type标签需要设置为“inputMethod”,srcEntry标签表示当前InputMethodExtensionAbility组件所对应的代码路径。
```ts ```ts
...@@ -410,4 +410,4 @@ ...@@ -410,4 +410,4 @@
针对InputMethodExtensionAbility开发,有以下相关实例可供参考: 针对InputMethodExtensionAbility开发,有以下相关实例可供参考:
- [Kika输入法](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/InputMethod/KikaInput) - [Kika输入法](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/InputMethod/KikaInput)
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册