HarmonyOS 验证码输入样式

如何实现图中验证码输入样式

HarmonyOS  验证码输入样式  -鸿蒙开发者社区

HarmonyOS
2024-12-24 15:52:14
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

输入验证码样式请参考下面demo:

CustomKeyboard.ets 页面

router.pushUrl({ url: ‘pages/CustomKeyboard’ }) 跳转即可测试

export interface commonBorder {
  width?: Length | EdgeWidths,
  color?: ResourceColor | EdgeColors,
  radius?: Length | BorderRadiuses,
  style?: BorderStyle | EdgeStyles
}

@Entry
@Component
struct CustomKeyboard {
  // 展示的text
  @State @Watch('setValue') codeTxt: string = '';
  // 屏幕宽度
  @State screenWidth: number = 0;
  @State isShow: boolean = false
  @State inputValue: string = ''
  // 最大长度
  @State maxLength: number = 6;
  @State Index: number[] = [0, 1, 2, 3, 4, 5]
  // 控制动画的
  @State showMouse: boolean[] = [];
  controller: TextInputController = new TextInputController()
  // 未选中的样式
  private btnCancelBorder?: commonBorder =
    { width: '3px', color: '#ccc', style: BorderStyle.Solid }
  // 选中的样式
  private btnCancelBorderActive: commonBorder =
    { width: '3px', color: Color.Red, style: BorderStyle.Solid }

  aboutToAppear(): void {
    console.info('IsaKit aboutToAppear')
    this.screenWidth = 1080;
    this.showMouse = new Array(this.maxLength).fill(false);
  }

  setValue() {
    if (this.codeTxt) {
      this.handelInputTxt(this.codeTxt.length, this.codeTxt);
    } else {
      this.handelInputTxt(0, '');
    }
  }

  // 改变输入框选中的值
  handelInputTxt(length: number, val: string) {
    length === this.maxLength ? this.showMouse.fill(false) : this.showMouse.fill(false).splice(length - 1, 0, true);
    console.log('----length', length)
    console.info('----this.showMouse', JSON.stringify(this.showMouse));
    console.log('---value', val)
  }

  // 自定义键盘样式
  @Builder
  CustomKeyboardBuilder() {
    Column() {
      Grid() {
        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => {
          GridItem() {
            Button(item + '')
              .width(110).onClick(() => {
              if (this.codeTxt.length >= 6) {
                return
              }
              this.codeTxt += item
            })
          }
        })
      }.maxCount(3).columnsGap(10).rowsGap(10).padding(5)
    }.backgroundColor(Color.Gray)

    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceEvenly }) {
      Button('关闭').width('30%')
        .onClick(() => {
          // 关闭自定义键盘
          this.controller.stopEditing()
        })
      Button().width('30%').opacity(0)

      Button('删除').width('30%')
        .onClick(() => {
          this.codeTxt = this.codeTxt.slice(0, -1)
          console.info('this.inputText', 'deleteLeft  this.inputText===' + this.codeTxt)
        })
    }
    .padding(5)
  }

  // 验证码的样式
  @Builder
  buildAEnterCodeInput() {
    Flex({
      direction: FlexDirection.Row,
      alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.SpaceBetween
    }) {
      Column() {
        Row() {
          Text(this.codeTxt[0])
            .fontSize(18)
            .fontWeight(600)
            .textAlign(TextAlign.Center)
            .width(50)
            .height(50)
            .margin({ left: 5, right: 5 })
            .border(this.showMouse[this.Index[0]] ? this.btnCancelBorderActive : this.btnCancelBorder)
            .borderRadius(5)

          Text(this.codeTxt[1])
            .fontSize(18)
            .fontWeight(600)
            .textAlign(TextAlign.Center)
            .width(50)
            .height(50)
            .margin({ left: 5, right: 5 })
            .border(this.showMouse[this.Index[1]] ? this.btnCancelBorderActive : this.btnCancelBorder)
            .borderRadius(5)

          Text(this.codeTxt[2])
            .fontSize(18)
            .fontWeight(600)
            .textAlign(TextAlign.Center)
            .width(50)
            .height(50)
            .margin({ left: 5, right: 5 })
            .border(this.showMouse[this.Index[2]] ? this.btnCancelBorderActive : this.btnCancelBorder)
            .borderRadius(5)

          Text(this.codeTxt[3])
            .fontSize(18)
            .fontWeight(600)
            .textAlign(TextAlign.Center)
            .width(50)
            .height(50)
            .margin({ left: 5, right: 5 })
            .border(this.showMouse[this.Index[3]] ? this.btnCancelBorderActive : this.btnCancelBorder)
            .borderRadius(5)

          Text(this.codeTxt[4])
            .fontSize(18)
            .fontWeight(600)
            .textAlign(TextAlign.Center)
            .width(50)
            .height(50)
            .margin({ left: 5, right: 5 })
            .border(this.showMouse[this.Index[4]] ? this.btnCancelBorderActive : this.btnCancelBorder)
            .borderRadius(5)

          Text(this.codeTxt[5])
            .fontSize(18)
            .fontWeight(600)
            .textAlign(TextAlign.Center)
            .width(50)
            .height(50)
            .margin({ left: 5, right: 5 })
            .border(this.showMouse[this.Index[5]] ? this.btnCancelBorderActive : this.btnCancelBorder)
            .borderRadius(5)
        }
        .onClick(() => {
          // sendEventByKey('TextInput', 10, '')
          focusControl.requestFocus('TextInput')
        })

        TextInput({ controller: this.controller, text: $$this.inputValue })
          .width('100%')
          .height(100)
          .opacity(0)
          .id('TextInput')
          .customKeyboard(this.CustomKeyboardBuilder())
          .onChange((value) => {
            this.codeTxt = value
            if (this.codeTxt.length >= 6) {
              return
            }
            this.codeTxt += this.inputValue;
            console.info('----codeTxt' + this.codeTxt)
          })

      }
    }
    .backgroundColor(Color.White)
    .height(50)
    .margin({ left: 15, right: 15 })
    .id('customInput')
    .defaultFocus(false)

  }

  build() {
    Column() {
      Blank().height(138)
      this.buildAEnterCodeInput()
      Blank().height(20)
    }
    .margin({ top: 10 })
    .borderRadius(8)
    .backgroundColor(Color.White)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
分享
微博
QQ
微信
回复
2024-12-24 18:06:36
相关问题
HarmonyOS 申请验证码demo实现
815浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
1287浏览 • 1回复 待解决
实现验证码登录之前的滑动验证实现
797浏览 • 1回复 待解决
前端验证码配合后端的实现思路?
4145浏览 • 1回复 待解决
密码或验证码登录页面完整代码
3536浏览 • 1回复 待解决
如何实现一个验证码弹窗子窗口
1211浏览 • 1回复 待解决