#鸿蒙通关秘籍#如何管理自定义安全键盘的按键事件?

HarmonyOS
5天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
墨舞涯SKU

在父组件中定义好键盘按键事件响应函数onKeyboardEvent,并将其传递给子组件。在子组件中使用@Link装饰器定义inputValue,这样可以保证父子组件之间的数据双向更新。

typescript @Component export struct CustomSafeKeyboardView { @State inputValue: string = ''; @State items: IKeyAttribute[] = numericKeyData; @State curKeyboardType: EKeyboardType = EKeyboardType.NUMERIC; controller: TextInputController = new TextInputController();

onKeyboardEvent(item: IKeyAttribute) { switch (item.type) { case EKeyType.INPUT: this.inputValue += item.value; break; case EKeyType.DELETE: this.inputValue = this.inputValue.slice(0, -1); break; case EKeyType.NUMERIC: if (this.curKeyboardType !== EKeyboardType.NUMERIC) { this.curKeyboardType = EKeyboardType.NUMERIC; this.items = numericKeyData; } break; case EKeyType.CAPSLOCK: if (this.curKeyboardType === EKeyboardType.LOWERCASE) { this.curKeyboardType = EKeyboardType.UPPERCASE; this.items = upperCaseKeyData; } else { this.curKeyboardType = EKeyboardType.LOWERCASE; this.items = lowerCaseKeyData; } break; case EKeyType.SPECIAL: ... } } }

分享
微博
QQ
微信
回复
5天前
相关问题
HarmonyOS 自定义键盘
237浏览 • 1回复 待解决