HarmonyOS 使用Navigation后,点击textinput组件,键盘弹起导致Textinput遮挡

使用navigation导航组件后,navigation里面的内容如果有Textinput,点击textinput弹出键盘,但是textinput并没有向上滑动,而是被遮挡了

这是navigaiton的特性吗?

HarmonyOS
2024-12-24 16:05:59
817浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

需要给对应的组件设置 expandSafeArea,这样就能实现效果

参考demo:

import { router } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State list: Array<number> = [];
  pathStack: NavPathStack = new NavPathStack()
  aboutToAppear(): void {
    //如果listcount=15点击列表的第14个输入框 键盘弹出就不会将第14个textinput弹起,如果将listcount改成100 点击第14个就将第14个textinput弹起
    let listCount = 25
    for (let index = 0; index < listCount; index++) {
      this.list = this.list.concat(index)
    }
  }

  build() {
    Navigation(this.pathStack) {
      this.buildContent()
    }
    .navBarPosition(NavBarPosition.End)
    .hideTitleBar(true)
  }

  @Builder
  buildContent(){
    Column() {
      List() {
        ForEach(this.list, (index: number, item) => {
          ListItem() {
            TextInput({ placeholder: `输入框${index}` })
          }
          .onClick(() => {

          })
          .width('60%')
          .height(50)
        })
      }
      .expandSafeArea([SafeAreaType.KEYBOARD])
    }
    .width('100%')
    .height('100%')
  }
}
  • 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.

setKeyboardAvoidMode 控制虚拟键盘抬起时页面的避让模式

// EntryAbility.ets
import { KeyboardAvoidMode } from '@ohos.arkui.UIContext';

onWindowStageCreate(windowStage: window.WindowStage) {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Index', (err, data) => {
    let keyboardAvoidMode  = windowStage.getMainWindowSync().getUIContext().getKeyboardAvoidMode();
    // 设置虚拟键盘抬起时压缩页面大小为减去键盘的高度
    windowStage.getMainWindowSync().getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.RESIZE);
    if (err.code) {
      hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

可参考案例3:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-expand-safe-area-V5#示例3

分享
微博
QQ
微信
回复
2024-12-24 18:38:56


相关问题
HarmonyOS TextInput组件弹起键盘
508浏览 • 1回复 待解决
HarmonyOS TextInput组件使用
558浏览 • 1回复 待解决
HarmonyOS textinput键盘弹出问题
1273浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘
1246浏览 • 1回复 待解决
HarmonyOS TextInput键盘监听
797浏览 • 1回复 待解决
HarmonyOS TextInput调用系统键盘问题
824浏览 • 1回复 待解决
HarmonyOS TextInput键盘相关问题咨询
1171浏览 • 1回复 待解决
HarmonyOS TextInput如何主动弹出键盘
1000浏览 • 1回复 待解决
HarmonyOS TextInput 组件问题
1225浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘问题
1479浏览 • 1回复 待解决