
##HarmonyOS Next快速入门##HarmonyOS应用开发##教育##
TextInput组件用于输入单行文本,使用非常广泛,例如应用登录账号密码、发送消息等。
TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})设置输入类型
.type(value: InputType)InputType枚举类型:
获取输入文本
设置onChange事件,输入文本发生变化时触发onChange事件执行回调函数。
.onChange(callback: EditableTextOnChangeCallback)代码实例:TextInputPage
@Entry
@Component
struct TextInputPage {
@State message: string = '第2节 TextInput组件';
@State phone:string='';
build() {
Column({space:6}) {
Text(this.message)
.fontSize(30)
.fontWeight(FontWeight.Bold)
TextInput({placeholder:'请输入账号'})
TextInput({text:'设置初始值'})
TextInput({placeholder:'请输入密码'})
.type(InputType.Password)
TextInput({placeholder:'请输入手机号'})
.type(InputType.Number)
.onChange((value:string)=>{
this.phone=value
})
Text('手机号码是:'+this.phone)
}
.height('100%')
.width('100%')
}
}设置当通过InputCounterOptions输入的字符数超过阈值时显示计数器。
showCounter(value: boolean, options?: InputCounterOptions)代码实例:通过maxLength、showCounter、showUnderline属性实现了计数器的功能。
@Entry
@Component
struct TextInputPage2 {
@State text: string = '';
controller: TextInputController = new TextInputController();
build() {
Column() {
TextInput({ text: this.text, controller: this.controller })
.placeholderFont({ size: 16, weight: 400 })
.width(336)
.height(56)
.maxLength(6)
.showUnderline(true)
.showCounter(true,
{ thresholdPercentage: 50, highlightBorder: true })//计数器显示效果为用户当前输入字符数/最大字符限制数。最大字符限制数通过maxLength()接口设置。
//如果用户当前输入字符数达到最大字符限制乘50%(thresholdPercentage)。字符计数器显示。
//用户设置highlightBorder为false时,配置取消红色边框。不设置此参数时,默认为true。
.onChange((value: string) => {
this.text = value;
})
}.width('100%').height('100%').backgroundColor('#F1F3F5')
}
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。