ArkUI TextInput组件

TextInput

根据组件名字,可以得知他是一个文本输出框。

声明代码👇

TextInput({placeholder?:ResourceStr,text?:ResourceStr});

placeholder: 就是提示文本,跟网页开发中的placeholder一样的

text:输入框当前的文本内容
特殊属性:

type(inputType.xxx). 可以决定输入框的类型,xxx的值有Normal、password(密码,会加密)、Email(邮箱格式)、Number(纯数字)等

注意: 只做约束,不做校验。
输入框可以使用事件来控制用户的交互操作。

测试

使用placeholder来控制未输入时的提示信息

使用type控制输入的类型,比如使用密码

当然,我们也可以是对他设置基本样式,比如背景色,宽度等

最重要的,我们可以通过事件来处理逻辑

TypeScript 复制代码
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Image($r('app.media.hongmeng'))
          .width(250)
        Text($r('app.string.width_label'))
          .fontSize(30)
          .fontWeight(FontWeight.Medium)
        TextInput({
          placeholder:'请输入图片宽度:'
        })
          .type(InputType.Password)
          .width(300)
          .backgroundColor("#ff0000")
          .onChange(e=>{
            console.log(e)
          })
      }

      .width('100%')
    }
    .height('100%')
  }
}

通过事件交互进行图片宽度的改变

我们通过交互事件将用户输入的数字大小赋值给imageWidth变量,再将image组件的width变成响应式的变量来完成这一操作。不过在其中要注意类型的转换。因为textinput的text属性需要的是一个字符串类型的,但是imageWidth是一个数字类型的,所以在使用的时候要考虑类型的转换。这里我使用了Number()和toString()强转。与javascript的语法相似。

TypeScript 复制代码
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @State imageWidth: number = 30
  build() {
    Row() {
      Column() {
        Image($r('app.media.hongmeng'))
          .width(this.imageWidth)
        Text($r('app.string.width_label'))
          .fontSize(30)
          .fontWeight(FontWeight.Medium)
        TextInput({
          text:this.imageWidth.toString()
        })
          .type(InputType.Number)
          .width(150)
          .backgroundColor("#ff0000")
          .onChange(e=>{
            this.imageWidth = Number(e)
          })
      }

      .width('100%')
    }
    .height('100%')
  }
}
相关推荐
90后的晨仔18 小时前
鸿蒙开发必备:macOS 上 ohpm 的完整安装与配置指南(从报错到成功)
harmonyos
90后的晨仔18 小时前
安装ohpm报错ERROROR: node: command not found Failed to find the executable 'node'
harmonyos
盐焗西兰花19 小时前
鸿蒙学习实战之路-Web 页面适配最佳实践
前端·学习·harmonyos
90后的晨仔19 小时前
ANDROID_HOME not set. Set the environment variable. Then, close DevEco Studio an
harmonyos
俩毛豆20 小时前
【鸿蒙生态共建】一文讲清耗时操作对定时器的超时事件影响及解决方案-《精通HarmonyOS NEXT :鸿蒙App开发入门与项目化实战》读者福利
华为·harmonyos
赵财猫._.20 小时前
【Flutter x 鸿蒙】第七篇:性能优化与调试技巧
flutter·性能优化·harmonyos
Oflycomm20 小时前
华为南京研究所通感一体智慧园区项目入选 WAA 优秀案例,行业数智化转型再迎新标杆
华为·wifi7·wifi7模块
2401_8603195221 小时前
【精通篇】打造React Native鸿蒙跨平台开发高级复合组件库开发系列:Badge 徽标(在右上角展示徽标数字或小红点)
react native·react.js·harmonyos
HONG````21 小时前
鸿蒙异步编程深度解析:async/await 原理、使用与实战
华为·harmonyos
小白考证进阶中21 小时前
华为HCIA/HCIP/HCIE认证考试科目大全
华为·网络工程师·hcie·hcia·hcip·华为云认证·华为认证体系