当前位置:   article > 正文

鸿蒙HarmonyOS实战-ArkUI组件(TextInput/TextArea)_鸿蒙 textinput 只读

鸿蒙 textinput 只读

 一、TextInput/TextArea

TextInput和TextArea组件通常用于收集用户输入的文本数据。

TextInput组件通常用于单行文本的输入,它允许用户通过一个光标来输入文字,并支持多种样式和布局选项来提高用户体验。例如,在用户输入错误时可以显示错误消息或在用户输入时自动完成文本。

TextArea组件与TextInput类似,但允许用户输入多行文本,它通常具有更大的输入框和滚动条来浏览输入的文本。Textarea组件也支持多种样式和布局选项,例如自动调整输入区域的大小以适应输入的文本,以及支持大于输入区域的文本滚动。

无论是TextInput还是TextArea,它们都使用onChange事件来检测文本输入的变化,并将输入的文本作为属性传递到父组件或应用程序。这些组件也可以在需要时收集其他的表单数据,例如表单提交时需要发送的数据。

1.创建输入框

语法说明:

  1. TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})
  2. TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})

使用:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. build() {
  6. Column() {
  7. TextArea({text:"我是TextArea我是TextArea我是TextArea我是TextArea"}).width(300)
  8. }.width('100%')
  9. }
  10. }

在这里插入图片描述

2.设置输入框类型

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. build() {
  6. Column() {
  7. TextInput()
  8. .type(InputType.Normal)
  9. TextInput()
  10. .type(InputType.Password)
  11. }.width('100%')
  12. }
  13. }

在这里插入图片描述

3.自定义样式

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. build() {
  6. Column() {
  7. TextInput({placeholder:'我是提示文本'})
  8. TextInput({placeholder:'我是提示文本',text:'我是当前文本内容'})
  9. TextInput({placeholder:'我是提示文本',text:'我是当前文本内容'})
  10. .backgroundColor(Color.Pink)
  11. }.width('100%')
  12. }
  13. }

在这里插入图片描述

4.添加事件

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. build() {
  6. Column() {
  7. TextInput()
  8. .onChange((value: string) => {
  9. console.info(value);
  10. })
  11. .onFocus(() => {
  12. console.info('获取焦点');
  13. })
  14. }.width('100%')
  15. }
  16. }

在这里插入图片描述

5.案例

登录界面是一种用于认证用户身份的界面。当用户访问需要身份验证的网站、应用程序或系统时,他们通常需要输入其用户名和密码来登录。登录界面通常包括一个输入框,以便用户输入其用户名或电子邮件地址,以及一个密码输入框,用于输入其密码。有些登录界面甚至还可包括验证码输入框或其他安全信息,以提高安全性。

登录界面是Web和移动应用程序中常见的界面元素,因为它们允许应用程序和网站保护其用户的个人信息和数据。登录界面通常需要正确的用户名和密码才能访问应用程序或网站。登录后,应用程序或网站将与该用户关联,并在以后的访问中保持登录状态,使用户能够轻松地访问其个人信息和数据。

  1. @Entry
  2. @Component
  3. struct TextInputSample {
  4. build() {
  5. Column() {
  6. TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
  7. .onSubmit((EnterKeyType)=>{
  8. console.info(EnterKeyType+'输入法回车键的类型值')
  9. })
  10. TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
  11. .onSubmit((EnterKeyType)=>{
  12. console.info(EnterKeyType+'输入法回车键的类型值')
  13. })
  14. Button('Sign in').width(150).margin({ top: 20 })
  15. }.padding(20)
  16. }
  17. }

在这里插入图片描述

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

推荐阅读
相关标签