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%')
  }
}
相关推荐
IntMainJhy10 分钟前
【flutter for open harmony】第三方库「Flutter 聊天组件鸿蒙化适配与实战:从零搭建鸿蒙跨平台聊天页面」
flutter·华为·harmonyos
key_3_feng11 分钟前
HarmonyOS NEXT开发环境搭建深度方案
华为·harmonyos
苗俊祥16 分钟前
沐界浏览器-轻量 · 多标签 · 为鸿蒙设备打造的网页浏览体验*
华为·harmonyos·鸿蒙
jiejiejiejie_24 分钟前
Flutter for OpenHarmony 地图功能萌系实战指南:给 App 加上超萌 “小地图”✨
flutter·华为·harmonyos
南村群童欺我老无力.31 分钟前
鸿蒙网络请求的错误处理与超时管理
网络·华为·harmonyos
jiejiejiejie_35 分钟前
Flutter for OpenHarmony 页面导航与动效库适配小记复盘:让 App 又丝滑又灵动✨
flutter·华为·harmonyos
音视频牛哥1 小时前
鸿蒙NEXT 音视频推送端实践:基于SmartMediaKit实现RTMP推流、轻量级RTSP服务与实时录像
华为·harmonyos·smartmediakit·鸿蒙next rtmp推流·鸿蒙next 摄像头推流·鸿蒙采集摄像头推流·鸿蒙采集摄像头麦克风推rtmp
想你依然心痛1 小时前
HarmonyOS 6(API 23)实战:基于 Face AR & Body AR 的“空间交互设计工作台“——PC端手势驱动3D建模系统
ar·交互·harmonyos·悬浮导航·沉浸光感
liulian09161 小时前
Flutter 网络状态与内容分享库:connectivity_plus 与 share_plus 的 OpenHarmony 适配指南总结
flutter·华为·学习方法·harmonyos
IntMainJhy1 小时前
Flutter 引导页 Onboarding 第三方库 的鸿蒙化适配与实战指南
harmonyos