diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets index 8bf6fcbbbfa2df00f8ef67780d370682cb69f1a3..7beecd22320be597df272335cb030c5503537b70 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets @@ -100,6 +100,59 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { this.listenShowKeyboardHeight(); } + private keyboardHeightChangeCallback = (data: number) => { + console.info('Succeeded in enabling the listener for keyboard height changes. Data: ' + JSON.stringify(data)); + if (data == 0) { + this.showType = keyboardType.ENTRY + } else { + this.showType = keyboardType.NONE + } + } + + private insertTextCallback = (text: string) => { + Log.d(TextInputMethodHandlerImpl.TAG, "insertText: " + text); + this.mEditable.handleInsertTextEvent(text); + } + + private deleteLeftCallback = (length: number) => { + /// 暂时规避方案 + /// OS机制与Android不一致,需要去监听软键盘事件才能发送KeyEvent事件 + if (this.mEditable.getStringCache() == "") { + this.sendKeyboardEvent(KeyType.Down, KeyCode.KEYCODE_DEL) + this.sendKeyboardEvent(KeyType.Up, KeyCode.KEYCODE_DEL) + } + this.mEditable.handleDeleteEvent(false, length); + } + + private deleteRightCallback = (length: number) => { + this.mEditable.handleDeleteEvent(true, length); + } + + private sendFunctionKeyCallback = (functionKey: inputMethod.FunctionKey) => { + /// 临时规避缺少newline对应枚举类型问题 + if (functionKey.enterKeyType == NEWLINE_KEY_TYPE) { + this.mEditable.handleNewlineEvent(); + } else { + this.showType = keyboardType.DONE + } + this.mEditable.handleFunctionKey(functionKey); + } + + private sendKeyboardStatusCallback = (state: inputMethod.KeyboardStatus) => { + if (state == inputMethod.KeyboardStatus.HIDE) { + this.plugin.textInputChannel.onConnectionClosed(this.inputTarget.id); + if (this.showType != keyboardType.DONE) { + this.showType = keyboardType.TRANSFER + } + this.hideTextInput() + this.cancelListenKeyBoardEvent() + } + } + + private selectByRangeCallback = (range: inputMethod.Range) => { + this.mEditable.handleSelectByRange(range); + } + show(): void { this.showTextInput(); } @@ -182,14 +235,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { listenShowKeyboardHeight() { window.getLastWindow(getContext(this)).then(currentWindow => { try { - currentWindow.on('keyboardHeightChange', (data) => { - console.info('Succeeded in enabling the listener for keyboard height changes. Data: ' + JSON.stringify(data)); - if (data == 0) { - this.showType = keyboardType.ENTRY - } else { - this.showType = keyboardType.NONE - } - }); + currentWindow.on('keyboardHeightChange', this.keyboardHeightChangeCallback); } catch (exception) { console.error(`Failed to enable the listener for keyboard height changes. Cause code: ${exception.code}, message: ${exception.message}`); } @@ -200,7 +246,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { listenHideKeyboardHeight() { window.getLastWindow(getContext(this)).then(currentWindow => { try { - currentWindow.off('keyboardHeightChange'); + currentWindow.off('keyboardHeightChange', this.keyboardHeightChangeCallback); } catch (exception) { console.error(`Failed to enable the listener for keyboard height changes. Cause code: ${exception.code}, message: ${exception.message}`); } @@ -271,10 +317,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { listenKeyBoardEvent(): void { try { - this.inputMethodController.on('insertText', (text) => { - Log.d(TextInputMethodHandlerImpl.TAG, "insertText: " + text); - this.mEditable.handleInsertTextEvent(text); - }); + this.inputMethodController.on('insertText', this.insertTextCallback); } catch (err) { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe insertText:" + JSON.stringify(err)); this.cancelListenKeyBoardEvent(); @@ -282,15 +325,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } try { - this.inputMethodController.on('deleteLeft', (length) => { - /// 暂时规避方案 - /// OS机制与Android不一致,需要去监听软键盘事件才能发送KeyEvent事件 - if (this.mEditable.getStringCache() == "") { - this.sendKeyboardEvent(KeyType.Down, KeyCode.KEYCODE_DEL) - this.sendKeyboardEvent(KeyType.Up, KeyCode.KEYCODE_DEL) - } - this.mEditable.handleDeleteEvent(false, length); - }) + this.inputMethodController.on('deleteLeft', this.deleteLeftCallback) } catch (err) { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe deleteLeft:" + JSON.stringify(err)); this.cancelListenKeyBoardEvent(); @@ -298,9 +333,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } try { - this.inputMethodController.on('deleteRight', (length) => { - this.mEditable.handleDeleteEvent(true, length); - }) + this.inputMethodController.on('deleteRight', this.deleteRightCallback) } catch (err) { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe deleteRight:" + JSON.stringify(err)); this.cancelListenKeyBoardEvent(); @@ -308,15 +341,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } try { - this.inputMethodController.on('sendFunctionKey', (functionKey) => { - /// 临时规避缺少newline对应枚举类型问题 - if (functionKey.enterKeyType == NEWLINE_KEY_TYPE) { - this.mEditable.handleNewlineEvent(); - } else { - this.showType = keyboardType.DONE - } - this.mEditable.handleFunctionKey(functionKey); - }) + this.inputMethodController.on('sendFunctionKey', this.sendFunctionKeyCallback) } catch (err) { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe sendFunctionKey:" + JSON.stringify(err)); this.cancelListenKeyBoardEvent(); @@ -324,16 +349,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } try { - this.inputMethodController.on('sendKeyboardStatus', (state) => { - if (state == inputMethod.KeyboardStatus.HIDE) { - this.plugin.textInputChannel.onConnectionClosed(this.inputTarget.id); - if (this.showType != keyboardType.DONE) { - this.showType = keyboardType.TRANSFER - } - this.hideTextInput() - this.cancelListenKeyBoardEvent() - } - }) + this.inputMethodController.on('sendKeyboardStatus', this.sendKeyboardStatusCallback) } catch (err) { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe sendKeyboardStatus:" + JSON.stringify(err)); this.cancelListenKeyBoardEvent(); @@ -341,9 +357,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } try { - this.inputMethodController.on('selectByRange', (range: inputMethod.Range) => { - this.mEditable.handleSelectByRange(range); - }) + this.inputMethodController.on('selectByRange', this.selectByRangeCallback) } catch (err) { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe selectByRange:" + JSON.stringify(err)); this.cancelListenKeyBoardEvent(); @@ -373,12 +387,12 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } cancelListenKeyBoardEvent(): void { - this.inputMethodController.off('insertText'); - this.inputMethodController.off('deleteLeft'); - this.inputMethodController.off('deleteRight'); - this.inputMethodController.off('sendFunctionKey'); - this.inputMethodController.off('sendKeyboardStatus'); - this.inputMethodController.off('selectByRange'); + this.inputMethodController.off('insertText', this.insertTextCallback); + this.inputMethodController.off('deleteLeft', this.deleteLeftCallback); + this.inputMethodController.off('deleteRight', this.deleteRightCallback); + this.inputMethodController.off('sendFunctionKey', this.sendFunctionKeyCallback); + this.inputMethodController.off('sendKeyboardStatus', this.sendKeyboardStatusCallback); + this.inputMethodController.off('selectByRange', this.selectByRangeCallback); this.imcFlag = false; }