TextInput、RichEditor、TextArea避让方案相同,以下以TextInput为例:
唤起软键盘时,系统会自动向上移动页面让输入框TextInput位于键盘避让区的上方,但TextInput下方的组件会被遮挡,所以需要监听键盘高度变化,自行控制页面向上偏移的距离。
import window from '@ohos.window'; import { KeyboardAvoidMode } from '@kit.ArkUI'; @Entry @Component struct KeyboardAvoidDemo { @State text: string = ''; controller: TextInputController = new TextInputController(); @State offsetNum: number = 0; onPageShow(): void { // 获取当前窗口实例 window.getLastWindow(this.getUIContext().getHostContext()).then((curWindow) => { try { curWindow.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.NONE); // 关闭当前页面对键盘的自动避让 // 监听键盘高度变化,返回键盘高度,单位为像素px curWindow.on('keyboardHeightChange', (height) => { // 在键盘高度变化时改变状态变量offsetNum if (height > 0) { this.getUIContext().animateTo({ duration: 200, curve: Curve.Smooth }, () => { this.offsetNum = this.getUIContext().px2vp(height); // 页面向上偏移的距离为键盘的高度 }); } else { this.getUIContext().animateTo({ duration: 200, curve: Curve.Smooth }, () => { this.offsetNum = 0; // height为0说明键盘收起 }); } console.info(`Succeeded in enabling the listener for keyboard height changes. Data: ${height}`); }); } catch (exception) { console.error(`Failed to listen keyboard height. Cause: ${exception.code}, message: ${exception.message}`); } }).catch((err: string) => { console.error(`setWindowOrientation: Failed to obtain the top window. Cause: ${err}`); }); } build() { Column() { Row() { } .width('100%') .height(550); Column() { Text('这是一个文本组件text1,和输入框同层级'); TextInput({ text: this.text, controller: this.controller, placeholder: '请输入内容' }) .placeholderFont({ size: 16, weight: 400 }) .width('90%') .onChange((value: string) => { this.text = value; }); Text('这是一个文本组件text2,和输入框同层级'); } .justifyContent(FlexAlign.SpaceAround) .height('600px'); Text('这是一个文本组件text3') .width('100%') .textAlign(TextAlign.Center); } .width('100%') .height('100%') .offset({ bottom: this.offsetNum // 相对于页面底部偏移 }); } }