HarmonyOS 鸿蒙Next 弹窗与键盘
HarmonyOS 鸿蒙Next 弹窗与键盘
使用CustomDialogController自定义弹窗,在屏幕底部显示,弹窗中又输入框,点击输入框唤起键盘后,键盘会把弹窗往上顶起,如何避免此效果
3 回复
可参考下面的示例
[@Entry](/user/Entry)
[@Component](/user/Component)
struct SafeAreaPage2 {
[@State](/user/State) text: string = ''
controller: TextInputController = new TextInputController()
[@State](/user/State) panelHeight: string = '0'
build() {
Stack({ alignContent: Alignment.Bottom }) {
Column() {
Button('弹起/收起').onClick(() => {
if (this.panelHeight === '0') {
this.panelHeight = '85%'
}else {
this.panelHeight = '0'
this.controller.stopEditing()
}
})
}
.expandSafeArea([SafeAreaType.KEYBOARD])
.width('100%')
.height('100%')
CustomEdit()
.height(this.panelHeight)
.visibility(this.panelHeight === '0' ? Visibility.Hidden : Visibility.Visible)
.animation({ duration: 300 })
}
.width('100%')
.height('100%')
}
}
[@Component](/user/Component)
struct CustomEdit {
build() {
Column() {
Text('请评论')
.margin({ top: 10 })
TextArea({ placeholder: '请输入' })
.width('96%')
.backgroundColor(Color.White)
.borderWidth(1)
.borderColor('#666')
.borderRadius(10)
.margin({ top: 5,bottom:5})
.defaultFocus(true)
.layoutWeight(1)
.textAlign(TextAlign.Start)
}
.width('100%')
.borderRadius({topLeft:15,topRight:15})
.backgroundColor(Color.Pink)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
}
}
更多关于HarmonyOS 鸿蒙Next 弹窗与键盘的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html