ArkTs9验证码倒计时demo

一、简易验证码倒计时,需要调整一下样式,主题逻辑是:

        1、通过countdownDisplay判断显示内容,false问显示文字,true为显示数字,默认为false

        2、点击文字 countdownDisplay 置为true,并且调用this.setShowTime();获取倒计时,second这个字段控制倒计时显示

        3、setShowTime控制倒计时的执行结束

// 展示显示文字,还是倒计时
@State countdownDisplay: boolean = false

// 倒计时
@State second: number = 59

// setInterval的执行ID,停止的时候需要用到
@State intervalID: number = 0

// 代码正文
  build() {
    Column() {
      Column({ space: 10 }) {       
        this.MobilePhone()
        Divider()
        this.CodeLine()
        Divider()
      
      }
      .width(CommonConstants.FULL_PERCENT)
      .padding($r('app.float.vp_twelve'))

    }
    .width(CommonConstants.FULL_PERCENT)
    .borderRadius({ topLeft: 25, topRight: 25 })
    .backgroundColor(Color.White)
  }

  @Builder MobilePhone() {
    Row() {
      this.Login_Image($r('app.media.ic_mobile_phone'), $r('app.float.vp_twenty_four'), $r('app.float.vp_twenty_four'))

      TextInput({ placeholder: '请输入手机号码' })
        .Mobile_InputText_Style()
        .onChange((val) => {
          this.userMobile = val
        })
    }.width(CommonConstants.FULL_PERCENT)
  }

  @Builder CodeLine() {
    Row() {
      this.Login_Image($r('app.media.ic_code'), $r('app.float.vp_twenty_four'), $r('app.float.vp_twenty_four'))

      Stack({ alignContent: Alignment.End }) {
        TextInput({ placeholder: '请输入验证码' })
          .backgroundColor(Color.White)
          .type(InputType.Number)
          .onChange((val) => {
            this.code = val
          })


        if (this.countdownDisplay) {
          Button() {
            Text(JSON.stringify(this.second))
              .fontColor($r('app.color.log_out'))
              .fontSize($r('app.float.fp_twelve'))
          }.Login_Code_Button()
        } else {
          Button() {
            Text(this.codeText)
              .fontColor($r('app.color.log_out'))
              .fontSize($r('app.float.fp_twelve'))
          }.Login_Code_Button()
          .onClick(() => {
            this.getCodeInfo()
          })
        }
      }
      .layoutWeight(1)
      .margin({ right: 20 })
    }
    .width(CommonConstants.FULL_PERCENT)
  }

// 倒计时逻辑
  setShowTime() {
    this.intervalID = setInterval(() => {
      this.second--
      if (this.second === 0) {
        this.countdownDisplay = !this.countdownDisplay
        this.second = 59
        clearInterval(this.intervalID);
      }
    }, 1000)
  }

// 样式,根据自己的UI调整
@Extend(Button) function Login_Code_Button() {
  .width($r('app.float.vp_eighty'))
  .height($r('app.float.vp_twenty_eight'))
  .borderWidth($r('app.float.vp_one'))
  .borderRadius($r('app.float.vp_eight'))
  .borderColor($r('app.color.log_out'))
  .backgroundColor(Color.White)
  .type(ButtonType.Normal)
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值