HarmonyOS 自定义弹窗(CustomDialog)如何实现安全区域避让

1、键盘弹出时弹窗与键盘之间不要留有间隙。

2、键盘收起后弹窗应避让系统底部安全区域,以便用户可以点击弹窗底部的发布等按钮。

HarmonyOS
2024-12-25 14:03:39
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以监听键盘高度来修改弹窗的offset实现效果,参考示例如下:

import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@CustomDialog
@Component
export struct CommentInputView {
  // 点击发送按钮的回调
  onSendAction?: (comment: string) => void;
  // 输入框绑定的控制器
  textAreaController: TextAreaController = new TextAreaController();
  // 光标位置
  @State caretIndex?: number = undefined;
  // 输入框内容
  @State private text?: string = undefined;
  // 若需要自定义弹窗中传入多个其他弹窗的的Controller,当前自定义弹窗的controller定义需放在其他传入的controller后面
  customDialogController: CustomDialogController;
  private context = getContext(this) as common.UIAbilityContext;
  @State dialogOffset: number = 20
  @State textOffset: number = 0

  /**
   * 在指定位置插入字符串
   * @param str 原字符串
   * @param index 插入位置
   * @param char 要插入的字符串
   * @returns 插入后的新字符串
   */
  private insertChar(str: string, index: number, char: string): string {
    return str.slice(0, index) + char + str.slice(index);
  }

  aboutToAppear(): void {
    let windowClass: window.Window | undefined = undefined;
    try {
      let promise = window.getLastWindow(this.context);
      promise.then((data) => {
        windowClass = data;
        try {
          windowClass.on('keyboardHeightChange', (data) => {
            if (data != 0) {
              this.textOffset = 0
              this.dialogOffset = 20
            } else {
              this.textOffset = -20
              this.dialogOffset = 0
            }
            console.info('Succeeded in enabling the listener for keyboard height changes. Data: ' +
            JSON.stringify(data));
          });
        } catch (exception) {
          console.error('Failed to enable the listener for keyboard height changes. Cause: ' +
          JSON.stringify(exception));
        }
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
      }).catch((err: BusinessError) => {
        console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
      });
    } catch (exception) {
      console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
    }


  }

  build() {
    Stack() {
      Column({
        space: 8,
      }) {
        TextArea({
          text: this.text,
          placeholder: '欢迎大家积极评论,理性发言,友善讨论',
          controller: this.textAreaController,
        })
          .onChange((value) => {
            if (this.text !== value) {
              this.text = value;
            }
            if (this.caretIndex) {
              this.textAreaController.caretPosition(this.caretIndex);
              this.caretIndex = undefined;
            }
          })
          .fontSize(15)
          .fontColor(Color.Black)
          .placeholderColor(Color.Gray)
          .enterKeyType(EnterKeyType.Send)
          .backgroundColor(Color.White)
          .width('100%')
          .height(140)
          .borderRadius(0)
          .padding(0)
          .defaultFocus(true)
          .id('text_input')

        Row() {
          Row({
            space: 2,
          }) {
            this.EmojiBuilder('')
            this.EmojiBuilder('')
          }
          .backgroundColor(Color.Grey)
          .borderRadius(14)
          .height(28)

          Text('发布')
            .textAlign(TextAlign.Center)
            .fontColor(Color.Red)
            .fontSize(14)
            .backgroundColor(Color.Black)
            .borderRadius(14)
            .width(58)
            .height(28)
        }
        .alignItems(VerticalAlign.Center)
        .justifyContent(FlexAlign.SpaceBetween)
        .width('100%')
      }
      .offset({ x: 0, y: this.textOffset }) //2.2、弹窗位置
      .borderRadius({
        topLeft: 8,
        topRight: 8,
      })
      .padding({
        top: 16,
        left: 16,
        bottom: 8,
        right: 16,
      })
      .backgroundColor(Color.Red)
      .width('100%')
      .onClick(() => {
        // 拦截点击空白处
      })
    }
    .backgroundColor(Color.Green)
    .align(Alignment.Bottom)
    .onClick(() => {
      this.customDialogController.close();
    })
    .offset({ x: 0, y: this.dialogOffset }) //2.2、弹窗位置
  }

  @Builder
  private EmojiBuilder(emoji: string) {
    Text(emoji)
      .padding({
        left: 6,
        right: 6,
      })
      .height('100%')
      .onClick(() => {
        if (this.text) {
          this.caretIndex = this.textAreaController.getCaretOffset().index;
          this.text = this.insertChar(this.text, this.caretIndex, emoji);
          this.caretIndex += emoji.length;
        } else {
          this.text = emoji;
        }
      })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
分享
微博
QQ
微信
回复
2024-12-25 16:46:43
相关问题
HarmonyOS 安全区域出错
836浏览 • 1回复 待解决
HarmonyOS 安全区域失效
811浏览 • 1回复 待解决
HarmonyOS 安全区域问题
1038浏览 • 1回复 待解决
HarmonyOS scroll安全区域问题
903浏览 • 1回复 待解决
HarmonyOS WebView安全区域问题
648浏览 • 1回复 待解决
HarmonyOS 设置安全区域不生效
894浏览 • 1回复 待解决
HarmonyOS 如何获取手机安全区域高度
850浏览 • 1回复 待解决
HarmonyOS 页面底部流出安全区域
903浏览 • 1回复 待解决
HarmonyOS 视频组件无法扩展其安全区域
1141浏览 • 1回复 待解决
关于屏幕安全区域的问题咨询
1125浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog
801浏览 • 1回复 待解决