HarmonyOS 验证码输入样式

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

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

HarmonyOS
2天前
浏览
收藏 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)
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 申请验证码demo实现
83浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
563浏览 • 1回复 待解决
实现验证码登录之前的滑动验证实现
167浏览 • 1回复 待解决
前端验证码配合后端的实现思路?
3450浏览 • 1回复 待解决
密码或验证码登录页面完整代码
1766浏览 • 1回复 待解决
如何实现一个验证码弹窗子窗口
431浏览 • 1回复 待解决