HarmonyOS Navigation不规避软键盘?

如题:HarmonyOS Navigation不规避软键盘?

阅读 419
1 个回答
import { RouterManager } from 'router/Index';
import { ConfigurationConstant } from '@kit.AbilityKit';
import window from '@ohos.window';
import display from'@ohos.display';

@Component
struct RichEditorComponent {
  /*  @StorageProp('currentColorMode') @Watch('onColorModeChange') currentMode: number =
      ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;*/
  @State isDark_In: boolean = false
  private controller: RichEditorController = new RichEditorController()

  build() {
    Column() {
      RichEditor({ controller: this.controller })
        .defaultFocus(true)
        .padding(0)
        .width("90%")
        .borderStyle(BorderStyle.Solid)
        .borderColor(Color.Black)
        .borderWidth(1)
        .padding({ left: 10 })
        .onReady(() => {
          this.controller.setTypingStyle({
            fontSize: 18
            // fontColor: $r("app.color.promptAction_openCustomDialog_text") }) })
          })
        })
    }
  }
}

@Preview
@Component
export struct PrivacyDialog {
  // @Consume('pageInfo') pageStack : NavPathStack;
  private window?: window.Window
  @State isAgree: string = "Not Agree";
  @State keyBoardHeightVP: number = 0
  @State avoidHeight: number = 0
  @State disPlayHeigt : number = 0
  @State avoidIndicatorHeigt : number = 0

  aboutToAppear(): void {
    window.getLastWindow(getContext(this)).then((win) => {
      this.window = win
      if (this.window) {

        this.window.on('keyboardHeightChange', (data) => {
          console.info('Succeeded in enabling the listener for keyboard height changes. 键盘 Data: ' + data);
          this.keyBoardHeightVP = px2vp(data)
        });

        this.window.on("avoidAreaChange", () => {
          // 获取挖孔屏避让区域
          let cutOutInfo = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);
          // 得到挖孔区域距离顶部的偏移量和挖孔区域矩形高度
          this.avoidHeight =  px2vp(cutOutInfo.topRect.top + cutOutInfo.topRect.height);
          console.log("zzz data : " + this.avoidHeight);
        })
        // 正常进行获取挖孔屏挖孔区域矩形数据
        let cutOutInfo = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);
        this.avoidHeight = px2vp(cutOutInfo.topRect.top + cutOutInfo.topRect.height);
        console.log("data : " + this.avoidHeight);

        this.window.on("avoidAreaChange", () => {
          // 获取导航条避让区域
          let cutIndicatorOutInfo = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);
          // 得到导航条区域距离顶部的偏移量和挖孔区域矩形高度
          this.avoidIndicatorHeigt =  px2vp(cutIndicatorOutInfo.bottomRect.top + cutIndicatorOutInfo.bottomRect.height);
          console.log("avoidIndicatorHeigt data : " + this.avoidIndicatorHeigt);
        })
        //
        let cutIndicatorOutInfo = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
        this.avoidIndicatorHeigt = px2vp(cutIndicatorOutInfo.bottomRect.top + cutIndicatorOutInfo.bottomRect.height);
        console.log("avoidIndicatorHeigt : " + this.avoidIndicatorHeigt);
      }

      let displayClass: display.Display = display.getDefaultDisplaySync();
      this.disPlayHeigt = px2vp(displayClass.height) - this.avoidHeight
      console.log(`LC  this.disPlayHeigt = ${this.disPlayHeigt}`)
    })
  }

  build() {
    NavDestination() {

      /*Column() {
      }
      .width("100%")
      .height("100%")
      .backgroundColor('rgba(0,0,0,0.5)')*/

      /*Column() {
        Text("注册应用账号").fontSize(30).height('20%')
        Text("请您仔细阅读一下协议并同意,我们将全力保护您的个人信息安全,您可以使用账号登录APP。").height('40%')
        Divider()
        Row(){
          Button("《应用隐私政策》", {type: ButtonType.Normal, stateEffect: true, buttonStyle:ButtonStyleMode.TEXTUAL })
            .onClick(ent => {
            // let pathInfo : NavPathInfo = new NavPathInfo('PrivacyItem', null
            //   , (popInfo: PopInfo) => {
            //     this.isAgree = popInfo.result.toString();
            //   })
            RouterManager.pushPath("privacyItem", null, (popInfo: PopInfo) => {
              this.isAgree = popInfo.result.toString();
            }, false)
          })
          Text(this.isAgree)
        }.height('20%')
        TextInput()
          .backgroundColor(Color.Green)
          .height(150)
        Divider()
        Row(){
          Button("不同意").onClick(ent => {
            RouterManager.pop("Not Agree", false)
          }).width('30%')
          Button("同意").onClick(ent => {
            RouterManager.pop("Agree", false)
          }).width('30%')
        }.height('20%')
        .justifyContent(FlexAlign.SpaceEvenly)
        .width('100%')
      }.backgroundColor(Color.White)
      .width('80%')
      .height('50%')*/

      Column() {
        Column(){
          RichEditorComponent()
        }
        .height(this.disPlayHeigt - 30 - this.keyBoardHeightVP)


        Row() {
          Button('照片')
            .height(30)
            .width('33%')

          Button('视频')
            .height(30)
            .width('33%')

          Button('链接')
            .height(30)
            .width('33%')
        }

        .position({x:0,y:this.disPlayHeigt-32})
        .translate({x:0,y:-this.keyBoardHeightVP})
        .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]) // SafeAreaType.SYSTEM,

      }.height('100%').width('100%')
      .backgroundColor(Color.White)
    }.hideTitleBar(true)
    .mode(NavDestinationMode.DIALOG)
    .onReady((ctx) => {
      console.log("==== PrivacyDialog onReady");
    })

  }
}

@Builder
export function getPrivacyDialog(): void {
  PrivacyDialog();
}

// DynamicsRouter.registerRouterPage(RouterInfo.PRIVACY_DIALOG, wrapBuilder(getPrivacyDialog));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进