【HarmonyOS】鸿蒙 TextInput 值获取方法
作者:zhongcx
- 2024-10-11 广东
本文字数:835 字
阅读完需:约 3 分钟
【HarmonyOS】记录当引入自定义组件有 TextInput 时,获取子组件输入框的值方法【1】使用 @Link【2】使用 onChange 回调
【方案一】使用 @Link
@Componentstruct LoginEditCompoments { @Link inputValue :string
build() { TextInput({text:$$this.inputValue}) }}
@Entry@Componentstruct Page04 { @State inputValue_1: string = "" @State inputValue_2: string = ""
build() { Column({ space: 10 }) { LoginEditCompoments({ inputValue:this.inputValue_1 }) LoginEditCompoments({ inputValue:this.inputValue_2 }) Button('登录').onClick(() => { console.info(`inputValue_1:${this.inputValue_1}`) console.info(`inputValue_2:${this.inputValue_2}`) }) } .height('100%') .width('100%') }}复制代码
打印
inputValue_1:555inputValue_2:7复制代码
【2】使用 onChange 回调
@Componentstruct LoginEditCompoments { onTextChange?: (value: string) => void
build() { TextInput().onChange((value: string) => { if(this.onTextChange){ this.onTextChange(value) } }) }}
@Entry@Componentstruct Page04 { @State inputValue_1: string = "" @State inputValue_2: string = ""
build() { Column({ space: 10 }) { LoginEditCompoments({ onTextChange: (value) => { this.inputValue_1 = value } }) LoginEditCompoments({ onTextChange: (value) => { this.inputValue_2 = value } }) Button('登录').onClick(() => { console.info(`inputValue_1:${this.inputValue_1}`) console.info(`inputValue_2:${this.inputValue_2}`) }) } .height('100%') .width('100%') }}复制代码
打印
inputValue_1:777inputValue_2:5复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
zhongcx
关注
还未添加个人签名 2024-09-27 加入
还未添加个人简介









评论