当前位置:   article > 正文

鸿蒙开发之ArkUI组件常用组件文本输入_鸿蒙开发,将输入框输入的信息显示在界面上

鸿蒙开发,将输入框输入的信息显示在界面上

TextInput、TextArea是输入框组件,通常用于响应用户的输入操作,比如评论区的输入、聊天框的输入、表格的输入等,也可以结合其它组件构建功能页面,例如登录注册页面。

TextInput为单行输入框、TextArea为多行输入框

TextArea

多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。

高度未设置时,组件无默认高度,自适应内容高度。宽度未设置时,默认撑满最大宽度。

接口  

TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})

参数:  

placeholder 是占位符,ResourceStr (分为string字符串类型和Resource资源引用类型,引入系统资源或者应用资源中的字符串)

text是文本内容,也是ResourceStr类型的

controller 设置TextArea控制器,对于TextAreaController 类型,官方给出的解释

TextArea组件的控制器,目前可通过它设置TextArea组件的光标位置,调用caretPosition(value: number) 设置光标位置

属性

        除支持通用属性外,还支持以下属性:

通用属性padding的默认值为:

{

top: 8 vp,

right: 16 vp,

bottom: 8 vp,

left: 16 vp

}

事件

除支持通用事件外,还支持以下事件:

案例
  1. @Entry
  2. @Component
  3. struct Index {
  4. @State message: string = 'Hello World'
  5. @State value:string = "123"
  6. @State placeholder:string = "palceholder"
  7. @State text: string = ''
  8. controller: TextAreaController = new TextAreaController()
  9. build() {
  10. Row() {
  11. Column() {
  12. TextArea({
  13. placeholder: 'input your word...',
  14. controller: this.controller
  15. })
  16. .placeholderColor("#00FF00")
  17. .placeholderFont({ size: 16, weight: 400 })
  18. .width(336)
  19. .height(56)
  20. .margin(20)
  21. .fontSize(16)
  22. .fontColor('#182431')
  23. .backgroundColor('#FFFFFF')
  24. .onChange((value: string) => {
  25. this.text = value
  26. })
  27. Text(this.text)
  28. Button('Set caretPosition 1')
  29. .backgroundColor('#007DFF')
  30. .margin(15)
  31. .onClick(() => {
  32. // 设置光标位置到第一个字符后
  33. this.controller.caretPosition(10)
  34. })
  35. }
  36. .width('100%')
  37. }
  38. .height('100%')
  39. }
  40. }

TextInput

单行文本输入框组件

接口

TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})

参数

参数placeholder和text 与TextArea是一样的

controller是TextInputController (TextInput组件的控制器),同样可以设置输入光标的位置

属性

除支持通用属性外,还支持以下属性:

其中 EnterKeyType枚举说明

InputType枚举说明

TextInputStyle9+枚举说明

事件

除支持通用事件外,还支持以下事件:

案例
  1. @Entry
  2. @Component
  3. struct TextInputExample {
  4. @State text: string = ''
  5. controller: TextInputController = new TextInputController()
  6. build() {
  7. Column() {
  8. TextInput({ text: this.text, placeholder: 'input your word...', controller: this.controller })
  9. .placeholderColor(Color.Grey)
  10. .placeholderFont({ size: 14, weight: 400 })
  11. .caretColor(Color.Blue)
  12. .width(400)
  13. .height(40)
  14. .margin(20)
  15. .fontSize(14)
  16. .fontColor(Color.Black)
  17. .inputFilter('[a-z]', (e) => {
  18. console.log(JSON.stringify(e))
  19. })
  20. .onChange((value: string) => {
  21. this.text = value
  22. })
  23. Text(this.text)
  24. Button('Set caretPosition 1')
  25. .margin(15)
  26. .onClick(() => {
  27. // 将光标移动至第一个字符后
  28. this.controller.caretPosition(1)
  29. })
  30. // 密码输入框
  31. TextInput({ placeholder: 'input your password...' })
  32. .width(400)
  33. .height(40)
  34. .margin(20)
  35. .type(InputType.Password)
  36. .maxLength(9)
  37. .showPasswordIcon(true)
  38. // 内联风格输入框
  39. TextInput({ placeholder: 'inline style' })
  40. .width(400)
  41. .height(50)
  42. .margin(20)
  43. .borderRadius(0)
  44. .style(TextInputStyle.Inline)
  45. }.width('100%')
  46. }
  47. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/887916
推荐阅读
相关标签
  

闽ICP备14008679号