针对跟随键盘的工具栏,可采用以下两种实现方案:
// 自定义对话框组件,用于显示输入框 @CustomDialog @Component export struct Dialog { dialogController: CustomDialogController; build() { Column() { TextInput({ text: '', placeholder: 'input your word...' }) .type(InputType.Normal) .defaultFocus(true) // 设置为默认聚焦状态 .onBlur(() => { // 输入框失去焦点时关闭对话框 this.dialogController.close(); }) .height('50%') .margin({ top: 25, left: 16, right: 16 }); } .height(100) .backgroundColor(Color.White); } } @Entry @Component struct Index { message: string = 'Hello World'; // 自定义对话框控制器,配置对话框的显示参数 customDialogController: CustomDialogController = new CustomDialogController({ builder: Dialog(), // 设置对话框内容组件 alignment: DialogAlignment.Bottom, // 对话框从底部弹出 cornerRadius: 0, customStyle: true, openAnimation: { duration: 0.001, tempo: 0 }, // 打开弹窗时动画设置 closeAnimation: { duration: 0.001, tempo: 0 }, // 关闭弹窗时动画设置 }); build() { Column() { // 点击按钮打开对话框 Text(this.message) .onClick(() => { this.customDialogController.open(); // 触发对话框显示 }) .id('HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold); } .height('100%') .width('100%'); } }
效果预览:

import { window } from '@kit.ArkUI'; @Entry @Component struct Index { message: string = '图文信息区域'; // 控制自定义工具栏组件可见性 @State isVisible: Visibility = Visibility.Hidden; // 存储键盘高度信息 @State keyboardHeight: string = '0px'; controller: RichEditorController = new RichEditorController(); // 页面加载时监听键盘高度变化 aboutToAppear(): void { window.getLastWindow(this.getUIContext().getHostContext()).then((win) => { win.on('keyboardHeightChange', (height) => { console.info('height' + height.toString()); if (height) { this.keyboardHeight = height.toString() + 'px'; // 保存键盘高度 this.isVisible = Visibility.Visible; // 显示自定义组件 } else { this.isVisible = Visibility.Hidden; // 隐藏自定义组件 } }); }); } build() { // 使用Stack布局,底部对齐 Stack({ alignContent: Alignment.Bottom }) { Column() { Text(this.message) .fontSize(52) .height('90%') .width('100%') .textAlign(TextAlign.Center) .fontColor(Color.Black); // 操作按钮行 Row() { Text('点击拉起键盘') .fontColor(Color.White) .width('90%') .height('40') .backgroundColor('#ff1a73f8') .borderRadius(20) .textAlign(TextAlign.Center) .margin({ left: 16, right: 16 }) .onClick(() => { this.isVisible = Visibility.Visible; // 显示自定义组件 setTimeout(() => { focusControl.requestFocus('textArea'); // 焦点切换到输入框 }, 10); }); }.height('10%').width('100%'); } .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM]); // 适配键盘区域 // 遮罩层(点击隐藏自定义) Column() .width('100%') .height('120%') .opacity(0.5) .backgroundColor('rgba(0,0,0,0.5)') .onClick(() => { this.isVisible = Visibility.Hidden; // 隐藏自定义组件 }) .visibility(this.isVisible) .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM, SafeAreaEdge.TOP]); // 动态加载自定义组件 CommentComponent({ isVisible: this.isVisible, keyboardHeight: this.keyboardHeight }); }; } // 页面卸载时隐藏自定义组件 aboutToDisappear(): void { this.isVisible = Visibility.Hidden; } } // 自定义工具栏组件 @Component struct CommentComponent { controller: RichEditorController = new RichEditorController(); @Link isVisible: Visibility; @Link keyboardHeight: string; build() { Flex({ direction: FlexDirection.ColumnReverse, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Stretch }) { Row() { RichEditor({ controller: this.controller }) .id('textArea') // 设置输入框ID用于焦点控制 .placeholder('喜欢就评论一下吧~', { fontColor: Color.Gray, font: { size: 16, weight: FontWeight.Normal } }) .width('65%'); Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.End, alignItems: ItemAlign.Stretch }) { Image($r('app.media.startIcon')).height(20).constraintSize({ minWidth: 20, maxWidth: 20 }); } .width(110); } .margin({ top: 10, bottom: this.keyboardHeight, // 根据键盘高度设置动态调整底部边距 left: 10, right: 10 }) .border({ width: 1 }) .borderRadius(50); } .width('100%') .height(380) .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM, SafeAreaEdge.TOP]) // 适配键盘区域 .backgroundColor(Color.White) .visibility(this.isVisible); } }
效果预览:
