HarmonyOS 自定义弹窗中,点击输入框,弹起键盘,输入框被顶出屏幕了?

如题:HarmonyOS 自定义弹窗中,点击输入框,弹起键盘,输入框被顶出屏幕了?

阅读 676
1 个回答

自定义弹窗仅适用于简单提示场景,不能替代页面使用。由于弹窗存在完全避让输入法行为,即在软键盘弹出时,会自动向上抬起软键盘高度,因此如果弹窗高度过大时,可能会导致部分区域不可见。建议使用模态框实现。 参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-sheet-transition-V5

参考demo:

@Entry
@Component
struct Index {
  @State message: string = 'CustomDialog中TextInput输入时,弹起键盘,整个弹窗内容被键盘挤上了。\n' +
    '期望页面固定,与键盘无关。帮忙看下这个问题怎么解决';
  @State isShow:Boolean = false
  @Builder
  BuildTop() {
    Row() {
      Row() {
        Image($r('app.media.dialog_ic_rank'))
          .objectFit(ImageFit.Contain)
          .height(23)
          .margin({ left: 6 })
          .onClick(() => {
          })
      }
      .width($r('app.float.default_63'))

      Text('设置')
        .fontSize($r('app.float.font_size_17'))
        .fontColor($r('app.color.col_101'))

      Row() {
        Button($r('app.string.set_finish'))
          .fontSize($r('app.float.font_size_14'))
          .backgroundColor($r('app.color.col_101'))
          .width($r('app.float.default_63'))
          .height(30)
      }
    }
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
    .alignItems(VerticalAlign.Center)
  }

  @Builder
  BuildTextEditor() {
    Column() {
      this.BuilderCommonTitle($r('app.string.set_text_editor'))

      TextInput({
        text: '',
        placeholder: $r('app.string.set_text_editor_placeholder')
      })
        .caretColor($r('app.color.app_green'))//光标颜色
        .caretStyle({ width: 2 })//光标风格,宽度
        .borderRadius(10)
        .height(50)
        .textAlign(TextAlign.Center)
        .fontSize($r('app.float.font_size_17'))
        .fontColor($r('app.color.col_101'))
        .backgroundColor('#F6F7F9')
        .margin({ left: 7, top: 10, right: 9 })
        .onChange((value) => {
          // this.text = value
        })
    }
    .width('100%')
    .margin({ top: 17 })
    .alignItems(HorizontalAlign.Start)
  }
  @Builder
  BuildSkin() {
    Column() {
      Text('皮肤列表区域')
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('40%')
    .margin({ top: 20 })
    .backgroundColor(Color.Gray)
  }

  @Builder
  BuilderBottomPlayMode() {
    Column() {
      this.BuilderCommonTitle($r('app.string.set_play_mode'))

      Row() {
        Button($r('app.string.set_play_mode_manual'))
          .width(115)
          .height(45)
          .margin({ left: 10 })
          .fontColor($r('app.color.col_101'))
          .fontSize($r('app.float.font_size_14'))
          .borderRadius(10)
          .backgroundColor($r('app.color.app_green'))
          .onClick(() => {
          })

        Button($r('app.string.set_play_mode_auto'))
          .width(115)
          .height(45)
          .margin({ left: 12 })
          .fontSize($r('app.float.font_size_14'))
          .fontColor($r('app.color.col_101'))
          .borderRadius(10)
          .backgroundColor($r('app.color.app_green'))
          .onClick(() => {
          })
      }
      .margin({ top: 11 })
    }
    .width('100%')
    .margin({ top: 17 })
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  BuilderCommonTitle(title: string | Resource) {
    Text(title)
      .fontColor($r('app.color.col_101'))
      .fontSize($r('app.float.font_size_14'))
  }

  @Builder bindSheetModel() {
    Column() {
      this.BuildTop()
      Divider()
        .height(0.5)
        .backgroundColor($r('app.color.col_f5'))
        .margin({ top: 21, left: 4, right: 4 })
      this.BuildTextEditor()
      this.BuildSkin()
      this.BuilderBottomPlayMode()
    }
    .align(Alignment.End)
    .width('100%')
    .height('100%')
    .padding({ left: $r('app.float.default_16'), right: $r('app.float.default_16'), top: 21, bottom: 80 })
    .backgroundColor(Color.White)
    .borderRadius({ topLeft: $r('app.float.default_17'), topRight: $r('app.float.default_17') })
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(16)
      }
    }
  }
}