ArkUI TextInput组件

TextInput

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

声明代码👇

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

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

text:输入框当前的文本内容

特殊属性:

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

注意: 只做约束,不做校验。

输入框可以使用事件来控制用户的交互操作。 

测试

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

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

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

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

@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的语法相似。

@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%')
  }
}

  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web阿成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值