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)
  }
}
分享
微博
QQ
微信
回复
2025-01-09 17:35:55
相关问题
HarmonyOS 输入框软键盘问题
382浏览 • 1回复 待解决
HarmonyOS 设置安全区域生效
409浏览 • 1回复 待解决
HarmonyOS 安全区域失效
370浏览 • 1回复 待解决
HarmonyOS 安全区域出错
420浏览 • 1回复 待解决
HarmonyOS 安全区域问题
497浏览 • 1回复 待解决
HarmonyOS 如何控制输入框弹出键盘
479浏览 • 1回复 待解决
HarmonyOS 验证码输入样式
316浏览 • 1回复 待解决
HarmonyOS scroll安全区域问题
467浏览 • 1回复 待解决
HarmonyOS WebView安全区域问题
214浏览 • 1回复 待解决
HarmonyOS 软键盘变为非数字输入
223浏览 • 1回复 待解决