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%')
  }
}
相关推荐
加农炮手Jinx1 小时前
Flutter 组件 ubuntu_service 适配鸿蒙 HarmonyOS 实战:底层系统服务治理,构建鸿蒙 Linux 子系统与守护进程交互架构
flutter·harmonyos·鸿蒙·openharmony·ubuntu_service
里欧跑得慢1 小时前
Flutter 三方库 mobx_codegen — 自动化驱动的高性能响应式状态管理(适配鸿蒙 HarmonyOS Next ohos)
flutter·自动化·harmonyos
王码码20352 小时前
Flutter 三方库 login_client 的鸿蒙化适配指南 - 打造工业级安全登录、OAuth2 自动化鉴权、鸿蒙级身份守门员
flutter·harmonyos·鸿蒙·openharmony·login_client
星辰徐哥2 小时前
鸿蒙金融理财全栈项目——上线与运维、用户反馈、持续迭代
运维·金融·harmonyos
加农炮手Jinx2 小时前
Flutter 三方库 cloudflare 鸿蒙云边协同分发流适配精讲:直连全球高速存储网关阵列无缝吞吐海量动静态画像资源,构筑大吞吐业务级网络负载安全分流-适配鸿蒙 HarmonyOS ohos
网络·flutter·harmonyos
大雷神6 小时前
HarmonyOS APP<玩转React>开源教程十一:组件化开发概述
前端·react.js·harmonyos
国医中兴9 小时前
Flutter 三方库 dson 的鸿蒙化适配指南 - 极简的序列化魔法、在鸿蒙端实现反射式 JSON 映射实战
flutter·harmonyos·鸿蒙·openharmony
池央11 小时前
在鸿蒙上跑 AI Agent:JiuwenClaw-on-OpenHarmony 完整实战
人工智能·华为·harmonyos
互联网散修11 小时前
零基础鸿蒙应用开发第五节:基础数据类型与对象类型转换
华为·harmonyos·鸿蒙应用开发入门教程
弓.长.11 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-fast-image — 高性能图片加载组件
react native·react.js·harmonyos