HarmonyOS 鸿蒙Next 如何监听软键盘关闭
HarmonyOS 鸿蒙Next 如何监听软键盘关闭
我现在通过点击页面上一个按钮。弹出dialog并且弹出软键盘 (因为dialog里用textInput)现在点击软键盘上右上角的箭头会直接关闭软键盘,但是dialog没有关闭,如何监听软键盘关闭 实现关闭弹框
2 回复
监听软键盘的弹出/关闭,参考以下Demo:
import { inputMethod } from '[@kit](/user/kit).IMEKit';
import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';
import { window } from '[@kit](/user/kit).ArkUI';
let windowClass: window.Window | undefined = undefined;
[@Entry](/user/Entry)
[@Component](/user/Component)
struct CountModifier {
[@State](/user/State) count: number = 0;
onPageShow(): void {
console.log('11111')
window.getLastWindow(getContext()).then(lastWindow => {
lastWindow.on('keyboardHeightChange', (data) => {
console.log(JSON.stringify(data))
if (data > 0) {
console.log('弹出软键盘')
} else {
console.log('收起软键盘')
}
})
}).catch((err: BusinessError) => {
console.error('Failed to enable the listener for keyboard height changes. Cause: ' + JSON.stringify(err));
})
}
appearKeyBoard() {
let inputMethodController = inputMethod.getController();
try {
let textConfig: inputMethod.TextConfig = {
inputAttribute: {
textInputType: 0,
enterKeyType: 1
}
};
inputMethodController.attach(true, textConfig, (err: BusinessError) => {
if (err) {
console.error(`Failed to attach: ${JSON.stringify(err)}`);
return;
}
console.log('Succeeded in attaching the inputMethod.');
});
} catch(err) {
console.error(`Failed to attach: ${JSON.stringify(err)}`);
}
}
hiddenKeyBoard() {
let inputMethodController = inputMethod.getController();
inputMethodController.detach((err: BusinessError) => {
if (err) {
console.error(`Failed to detach: ${JSON.stringify(err)}`);
return;
}
console.log('Succeeded in detaching inputMethod.');
});
}
build() {
Column() {
Button('点击弹出软键盘')
.onClick(() => {
this.appearKeyBoard();
})
Text('--------------------')
Button('点击关闭软键盘')
.onClick(() => {
this.hiddenKeyBoard();
})
}
}
}