【关键字】
自定义密码弹框 / 弹起 / enableKeyboardOnFocus / textinput
【问题描述】
API 10手机升级到Canary2版本(API 11)后,自定义密码弹框无法自动弹起。
API 10的示例代码:
API 10的效果是textinput的enableKeyboardOnFocus这个方法如果设置为true,则键盘会自动弹起;如果设置为false,则键盘不会自动弹起。
【解决方案】
建议使用如下代码:
// xxx.ets
@Entry
@Component
struct TextInputExample {
controller: TextInputController = new TextInputController()
@State inputValue: string = ""
// 自定义键盘组件
@Builder CustomKeyboardBuilder() {
Column() {
Button('x').onClick(() => {
// 关闭自定义键盘
this.controller.stopEditing()
})
Grid() {
ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item:number|string) => {
GridItem() {
Button(item + "")
.width(110).onClick(() => {
this.inputValue += item
})
}
})
}.maxCount(3).columnsGap(10).rowsGap(10).padding(5)
}.backgroundColor(Color.Gray)
}
build() {
Column() {
TextInput({ controller: this.controller, text: this.inputValue })
// 绑定自定义键盘
.customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 })
.enableKeyboardOnFocus(true)
.defaultFocus(true)
}
}
}
关键是添加:.defaultFocus(true)