HarmonyOS 将验证码demo改成横屏后,软键盘弹出会挡住输入框,按照安全区域的文档,增加expandSafeArea,未生效

将验证码demo改成横屏后,软键盘弹出会挡住输入框,按照安全区域的文档,增加expandSafeArea,未生效。

文档中给的样例2,即使不增加.expandSafeArea([SafeAreaType.KEYBOARD, SafeAreaType.SYSTEM]),也会上移避让软键盘。请问验证码demo,如何修改,才可以上移避让软键盘

验证码demo:https://gitee.com/harmonyos_samples/verification-code-scenario

安全区域文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-expand-safe-area-V5#%E7%A4%BA%E4%BE%8B2

HarmonyOS
2025-01-09 16:33:01
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考以下代码,动态调整展示画面的高度,本代码中写死了绑定输入法的文字高度是200,实际使用时可以更精确的调整下

import { inputMethod } from '@kit.IMEKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { rcp } from '@kit.RemoteCommunicationKit';

@Entry
@Component
struct Index {
  @State articlePlaying: boolean = true;
  @State disPlayHeight: number = 0;
  @State avoidHeight: number = 0
  private codeIndexArray: Array<number> = Array.from([0, 1, 2, 3, 4, 5]);
  private inputController: inputMethod.InputMethodController = inputMethod.getController();
  @State codeText: string = '';
  private textConfig: inputMethod.TextConfig = {
    // 配置编辑框属性
    inputAttribute: {
      // 数字编辑框
      textInputType: inputMethod.TextInputType.NUMBER,
      // enterKeyType键类型
      enterKeyType: inputMethod.EnterKeyType.GO
    }
  }

  onPageShow(): void {
    // 获取当前应用内最上层的子窗口
    window.getLastWindow(getContext(this)).then(currentWindow => {
      // 获取当前窗口的属性
      let property = currentWindow.getWindowProperties();
      // 获取窗口内容规避的区域
      let avoidArea = currentWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
      // 初始化显示区域高度
      this.disPlayHeight = px2vp(property.windowRect.height);
      console.log("kljtest property.windowRect.height is " + property.windowRect.height)
      console.log("kljtest avoidArea.topRect.height is " + avoidArea.topRect.height)
      console.log("kljtest avoidArea.bottomRect.height is " + avoidArea.bottomRect.height)
      console.log("kljtest disPlayHeight is " + this.disPlayHeight)
      // 需避让高度
      this.avoidHeight = px2vp(avoidArea.topRect.height + avoidArea.bottomRect.height);
      console.log("kljtest avoidHeight is " + this.avoidHeight)
      // 监视软键盘的弹出和收起
      currentWindow.on('avoidAreaChange', async data => {
        if (data.type !== window.AvoidAreaType.TYPE_KEYBOARD) {
          return;
        }
        this.avoidHeight = px2vp(avoidArea.topRect.height + data.area.bottomRect.height);
        console.log("kljtest data.area.bottomRect.height is " + data.area.bottomRect.height)
        console.log("kljtest 2 avoidHeight is " + this.avoidHeight)
      })
    })
  }

  build() {
    Column() {
      Blank()
        .height(this.disPlayHeight - this.avoidHeight - 200)
      Row() {
        Text('dfsdafasdfadf')
      }
      .backgroundColor(Color.Orange)
      .defaultFocus(true)
      // 点击拉起软键盘
      .onClick(() => {
        this.inputController.attach(true, this.textConfig);
        const session = rcp.createSession();
        session.post("http://example.com/post", "data to send").then((response) => {
          console.info(`Response succeed: ${response}`);
        }).catch((err: BusinessError) => {
          console.error(`err: err code is ${err.code}, err message is ${JSON.stringify(err)}`);
        });
      })
    }
    .width('100%')
    .expandSafeArea([SafeAreaType.KEYBOARD])
    .height(this.disPlayHeight - this.avoidHeight)
    .backgroundColor(Color.Pink)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:35:55
相关问题
HarmonyOS 输入框软键盘问题
802浏览 • 1回复 待解决
HarmonyOS 设置安全区域生效
842浏览 • 1回复 待解决
HarmonyOS 安全区域出错
789浏览 • 1回复 待解决
HarmonyOS 安全区域失效
755浏览 • 1回复 待解决
HarmonyOS 安全区域问题
977浏览 • 1回复 待解决
HarmonyOS 如何控制输入框弹出键盘
959浏览 • 1回复 待解决
HarmonyOS WebView安全区域问题
603浏览 • 1回复 待解决
HarmonyOS scroll安全区域问题
843浏览 • 1回复 待解决
HarmonyOS 验证码输入样式
781浏览 • 1回复 待解决
HarmonyOS 软键盘变为非数字输入
499浏览 • 1回复 待解决