【HarmonyOS NEXT】解决自界说弹框和键盘之间安全隔断的问题 ...

打印 上一主题 下一主题

主题 916|帖子 916|积分 2748

【HarmonyOS NEXT】解决自界说弹框和键盘之间安全隔断的问题

一、问题背景


我们在应用开辟批评输入框时,常规的需求样式是:输入框view和键盘贴近,上半部展示信息区的形式,这样的计划,方便用户不割裂的去批评发言。
但是在利用鸿蒙提供的自界说弹框时,会发现键盘和弹框之间有个安全安定。就算弹框结构是置底,每次表现键盘都会将弹框顶上去。

自界说弹框源码
  1. @CustomDialog
  2. @Component
  3. struct CustomDialogExample {
  4.   @Link textValue: string
  5.   @Link inputValue: string
  6.   controller?: CustomDialogController
  7.   cancel: () => void = () => {
  8.   }
  9.   confirm: () => void = () => {
  10.   }
  11.   build() {
  12.     Column() {
  13.       TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
  14.         .onChange((value: string) => {
  15.           this.textValue = value
  16.         })
  17.         .defaultFocus(true)
  18.       Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
  19.       Flex({ justifyContent: FlexAlign.SpaceAround }) {
  20.         Button('cancel')
  21.           .onClick(() => {
  22.             if (this.controller != undefined) {
  23.               this.controller.close()
  24.               this.cancel()
  25.             }
  26.           }).backgroundColor(0xffffff).fontColor(Color.Black)
  27.         Button('confirm')
  28.           .onClick(() => {
  29.             if (this.controller != undefined) {
  30.               this.inputValue = this.textValue
  31.               this.controller.close()
  32.               this.confirm()
  33.             }
  34.           }).backgroundColor(0xffffff).fontColor(Color.Red)
  35.       }.margin({ bottom: 10 })
  36.     }.borderRadius(10)
  37.     .height(px2vp(500))
  38.     .offset({ x: 0, y: 16})
  39.   }
  40. }
复制代码
调用页面源码:
  1. @Entry
  2. @Component
  3. struct TextPage {
  4.   @State textValue: string = ''
  5.   @State inputValue: string = 'click me'
  6.   dialogController: CustomDialogController | null = new CustomDialogController({
  7.     builder: CustomDialogExample({
  8.       cancel: ()=> { this.onCancel() },
  9.       confirm: ()=> { this.onAccept() },
  10.       textValue: $textValue,
  11.       inputValue: $inputValue
  12.     }),
  13.     cancel: this.exitApp,
  14.     autoCancel: true,
  15.     onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
  16.       console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
  17.       console.log("dialog onWillDismiss")
  18.       if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
  19.         dismissDialogAction.dismiss()
  20.       }
  21.       if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
  22.         dismissDialogAction.dismiss()
  23.       }
  24.     },
  25.     keyboardAvoidMode: KeyboardAvoidMode.DEFAULT,
  26.     alignment: DialogAlignment.Bottom,
  27.     // offset: { dx: 0, dy: -20 },
  28.     gridCount: 4,
  29.     customStyle: false,
  30.     cornerRadius: 10,
  31.     backgroundColor: Color.Black
  32.   })
  33.   // 在自定义组件即将析构销毁时将dialogController置空
  34.   aboutToDisappear() {
  35.     this.dialogController = null // 将dialogController置空
  36.   }
  37.   onCancel() {
  38.     console.info('Callback when the first button is clicked')
  39.   }
  40.   onAccept() {
  41.     console.info('Callback when the second button is clicked')
  42.   }
  43.   exitApp() {
  44.     console.info('Click the callback in the blank area')
  45.   }
  46.   build() {
  47.     Column() {
  48.       Button(this.inputValue)
  49.         .onClick(() => {
  50.           if (this.dialogController != null) {
  51.             this.dialogController.open()
  52.           }
  53.         }).backgroundColor(0x317aff)
  54.     }.width('100%').margin({ top: 5 })
  55.   }
  56. }
复制代码
二、解决方案

相识安全弹框的问题,需要对自界说弹框的实现有比较深刻的熟悉才气规避。
起首我们要搞清晰,自界说弹框的根本用法,会发现在CustomDialogExample中,build是弹框结构的具体样式的一部分,在调用页面TextPage ,实在也会设置弹框的一些样式属性,比方:customStyle。
这是体系体系的根本样式,如果customStyle该属性为false,那我们的弹框样式,只需要关心弹框内的结构,整个弹框外围是交给体系定制样式处置惩罚。
所以当我们设置customStyle该属性为true,就会发现弹框view会贴合键盘,但是整体弹框的边框都没了。

此时我们只需要处置惩罚边框样式和弹框背景致即可:
.borderRadius(15)
.backgroundColor(Color.White)
.borderWidth(5)

源码示例:

自界说弹框源码
  1. @CustomDialog
  2. @Component
  3. struct CustomDialogExample {
  4.   @Link textValue: string
  5.   @Link inputValue: string
  6.   controller?: CustomDialogController
  7.   cancel: () => void = () => {
  8.   }
  9.   confirm: () => void = () => {
  10.   }
  11.   build() {
  12.     Column() {
  13.       TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
  14.         .onChange((value: string) => {
  15.           this.textValue = value
  16.         })
  17.         .defaultFocus(true)
  18.       Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
  19.       Flex({ justifyContent: FlexAlign.SpaceAround }) {
  20.         Button('cancel')
  21.           .onClick(() => {
  22.             if (this.controller != undefined) {
  23.               this.controller.close()
  24.               this.cancel()
  25.             }
  26.           }).backgroundColor(0xffffff).fontColor(Color.Black)
  27.         Button('confirm')
  28.           .onClick(() => {
  29.             if (this.controller != undefined) {
  30.               this.inputValue = this.textValue
  31.               this.controller.close()
  32.               this.confirm()
  33.             }
  34.           }).backgroundColor(0xffffff).fontColor(Color.Red)
  35.       }.margin({ bottom: 10 })
  36.     }.width("90%")
  37.     .borderRadius(15)
  38.     .backgroundColor(Color.White)
  39.     .borderWidth(5)
  40.     .height(px2vp(500))
  41.     .offset({ x: 0, y: 16})
  42.   }
  43. }
复制代码
调用页面源码:
  1. @Entry
  2. @Component
  3. struct TextPage {
  4.   @State textValue: string = ''
  5.   @State inputValue: string = 'click me'
  6.   dialogController: CustomDialogController | null = new CustomDialogController({
  7.     builder: CustomDialogExample({
  8.       cancel: ()=> { this.onCancel() },
  9.       confirm: ()=> { this.onAccept() },
  10.       textValue: $textValue,
  11.       inputValue: $inputValue
  12.     }),
  13.     cancel: this.exitApp,
  14.     autoCancel: true,
  15.     onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
  16.       console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
  17.       console.log("dialog onWillDismiss")
  18.       if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
  19.         dismissDialogAction.dismiss()
  20.       }
  21.       if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
  22.         dismissDialogAction.dismiss()
  23.       }
  24.     },
  25.     keyboardAvoidMode: KeyboardAvoidMode.DEFAULT,
  26.     alignment: DialogAlignment.Bottom,
  27.     gridCount: 4,
  28.     customStyle: true,
  29.     cornerRadius: 10,
  30.     backgroundColor: Color.Black
  31.   })
  32.   // 在自定义组件即将析构销毁时将dialogController置空
  33.   aboutToDisappear() {
  34.     this.dialogController = null // 将dialogController置空
  35.   }
  36.   onCancel() {
  37.     console.info('Callback when the first button is clicked')
  38.   }
  39.   onAccept() {
  40.     console.info('Callback when the second button is clicked')
  41.   }
  42.   exitApp() {
  43.     console.info('Click the callback in the blank area')
  44.   }
  45.   build() {
  46.     Column() {
  47.       Button(this.inputValue)
  48.         .onClick(() => {
  49.           if (this.dialogController != null) {
  50.             this.dialogController.open()
  51.           }
  52.         }).backgroundColor(0x317aff)
  53.     }.width('100%').margin({ top: 5 })
  54.   }
  55. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

道家人

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表