HarmonyOS 自定义键盘弹出,输入框内容被遮挡,系统键盘是正常的,如何解决?

自定义键盘弹出,输入框内容被遮挡,系统键盘是正常的,如何解决?

HarmonyOS
2024-10-29 10:58:01
1387浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

​您可以通过以下方案规避此问题:

可获取窗口内容规避的区域,规避区域的类型是软键盘区域TYPE_KEYBOARD,当软键盘弹出,获取规避区域的高度,通过margin-bottom来设置想要顶起组件。

参考文档:

https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/reference/apis/js-apis-window.md/#avoidareatype7

示例代码如下:​

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-10-29 17:52:04
相关问题
HarmonyOS 键盘遮挡输入框
725浏览 • 1回复 待解决
HarmonyOS 自定义键盘不能顶起输入框
1243浏览 • 1回复 待解决
HarmonyOS Toast弹出时候键盘遮挡
1290浏览 • 1回复 待解决
HarmonyOS 如何控制输入框弹出键盘
985浏览 • 1回复 待解决
HarmonyOS 自定义键盘输入框焦点问题
959浏览 • 1回复 待解决
HarmonyOS 自定义键盘弹起后遮住输入框
1497浏览 • 1回复 待解决
HarmonyOS webview里输入框键盘覆盖
704浏览 • 1回复 待解决
HarmonyOS web中输入框键盘遮住
729浏览 • 1回复 待解决
HarmonyOS 输入框屏蔽系统键盘
653浏览 • 1回复 待解决