关键字
监听 / 键盘 / 输入法框架 / 窗口
问题描述
是否有监听键盘显隐的方法?
解决方案
可选择以下任一方案:
方案一:
通过输入法框架模块(@ohos.inputMethod)来监听软键盘状态。
用InputMethodController实例的on('sendKeyboardStatus')方法来监听,直接在inputMethodController.on('sendKeyboardStatus', callback)的callback中处理。详细信息请参见:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inputmethod-0000001774281542#ZH-CN_TOPIC_0000001774281542__onsendkeyboardstatus10
方案二:
通过窗口模块(@ohos.window)来监听软键盘状态。
用Window实例的on('keyboardHeightChange')方法来监听软键盘高度,可以判断软键盘状态。
示例代码如下:
let windowClass: window.Window | undefined = undefined;
try {
window.getLastWindow(this.context, (err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
return;
}
windowClass = data;
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
windowClass.on('avoidAreaChange', (data) => {
console.info('Succeeded in enabling the listener for system avoid area changes. type:' + JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));
});
});
} catch (exception) {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}