HarmonyOS 软键盘弹出方式

自定义@CustomDialog中添加TextArea,在TextArea获取焦点时软键盘弹出会把整个自定义@CustomDialog。如何让软键盘弹出时不要顶起整个自定义@CustomDialog,而是顶起的高度只到TextArea获取焦点的位置。

HarmonyOS
2024-12-20 16:11:24
851浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

该问题是软键盘弹出的时候,软键盘会遮挡一部分组件的显示,需求是被遮挡部分想要被软键盘顶起来,在软键盘的上方。软件盘弹出的时候默认是顶起输入框,对于输入框以下的显示组件将被遮挡。可获取窗口内容规避的区域,规避区域的类型是软键盘区域TYPE_KEYBOARD,当软键盘弹出,获取规避区域的高度,通过margin-bottom来设置想要顶起组件。

代码示例:

import window from '@ohos.window';
@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller()
  private arr: number[] = [0, 1, 2, 3, 4, 5]
  @State scrollHeight: number = 0;
  @State isRebuild: boolean = false;
  @State keyHeight: number =0;
  @State text: string = ''
  aboutToAppear() {
    window.getLastWindow(getContext(this)).then(currentWindow =>{
      let property = currentWindow.getWindowProperties();
      // init window height
      let avoidArea = currentWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD);
      this.scrollHeight = px2vp(property.windowRect.height - avoidArea.bottomRect.height);
      // monitor the hiding and display of softKeyboard
      currentWindow.on('avoidAreaChange', async data => {
        if (data.type !== window.AvoidAreaType.TYPE_KEYBOARD) {
          this.keyHeight=avoidArea.bottomRect.height;
          return;
        }
        this.scrollHeight = px2vp(property.windowRect.height - data.area.bottomRect.height);
      })
    })
  }
  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Column() {
        Scroll(this.scroller) {
          Column() {
            TextInput({ text: this.text, placeholder: 'input your word...' })
              .placeholderFont({ size: 14, weight: 400 })
              .width(320)
              .height(40).margin(200)
              .fontSize(14)
              .fontColor(Color.Black)
              .backgroundColor(Color.White)
            ForEach(this.arr, (item:number) => {
              Text(item.toString())
                .width('90%')
                .height(150)
                .backgroundColor(0xFFFFFF)
                .borderRadius(15)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .margin({ top: 10 })
            })
          }.width('100%')
        }
        .width('100%').height(this.scrollHeight).layoutWeight(1)
        Text('这是一个测试文本')
          .width('100%')
          .height(50)
          .backgroundColor(Color.Red)
          .margin({bottom: this.scrollHeight})
      }.width('100%').height('100%').justifyContent(FlexAlign.Start)
    }.width('100%').height('100%').backgroundColor(0xDCDCDC)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 19:03:29


相关问题
HarmonyOS 如何监听软键盘弹出
875浏览 • 1回复 待解决
HarmonyOS 软键盘弹出隐藏监听
786浏览 • 1回复 待解决
关于软键盘弹出遮挡问题
1863浏览 • 1回复 待解决
如何判断软键盘是否弹出
2790浏览 • 1回复 待解决
HarmonyOS 如何代码控制软键盘弹出
1135浏览 • 1回复 待解决
HarmonyOS 软键盘弹出控制与检测
761浏览 • 1回复 待解决
HarmonyOS 软键盘弹出后又突然消失
665浏览 • 1回复 待解决
HarmonyOS 弹出软键盘时,web页面白屏
794浏览 • 1回复 待解决
软键盘弹出时,页面的自适应
2282浏览 • 1回复 待解决
window模拟器无法弹出软键盘
695浏览 • 1回复 待解决
HarmonyOS UI组件如何知道弹出软键盘
510浏览 • 1回复 待解决
HarmonyOS是否有弹出隐藏软键盘的api
637浏览 • 1回复 待解决
鸿蒙软键盘弹出后,页面底部的按钮
4901浏览 • 0回复 待解决
如何控制软键盘弹出对页面的遮挡?
3367浏览 • 1回复 待解决
HarmonyOS 软键盘操作API
534浏览 • 1回复 待解决
HarmonyOS TextInput软键盘监听
841浏览 • 1回复 待解决