请参考以下demoimport inputMethod from '@ohos.inputMethod'; @Entry @Component struct HideKeyboard { controller: TextInputController = new TextInputController(); build() { Column() { KeyBoard({controller:this.controller}) Button('关闭') .onClick(() => { this.controller.stopEditing() }) } } } @Component struct KeyBoard{ controller: TextInputController = new TextInputController(); @State inputValue: string = ''; @State show: boolean = false; // 自定义键盘组件 @Builder CustomKeyboardBuilder() { Column() { 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() { TextInput({ controller: this.controller, text: this.inputValue })// 绑定自定义键盘 .customKeyboard(this.CustomKeyboardBuilder()) .margin(10) .height(48) } }
请参考以下demo