HarmonyOS 想做一个输入验证码的功能,请问这个UI要怎么设计啊,有好的方案吗

HarmonyOS
2024-12-24 16:25:09
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

参考示例:

//Index.est
import { router } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Button('使用系统键盘')
          .onClick(() => {
            router.pushUrl({ url: 'pages/SystemKeyboard' })
          })
          .margin({ bottom: 20 })
        Button('使用自定义键盘')
          .onClick(() => {
            router.pushUrl({ url: 'pages/CustomKeyboard' })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
//CommonPage.ets
@Component
export struct CommonPage {
  build() {
    Row() {
      Column() {
        Navigation() {
          Row() {
            Text('登录账号')
            Text('156****5163')
          }
          .width('90%')
          .justifyContent(FlexAlign.SpaceBetween)
          .alignItems(VerticalAlign.Center)
          .padding({ bottom: 20 })
          .border({ width: { bottom: 1 }, color: '#ccc' })
          .margin({ top: 20, bottom: 20 })

          Row() {
            Text('切换账号')
            Image($r('app.media.right'))
              .width(20)
          }
          .width('90%')
          .justifyContent(FlexAlign.SpaceBetween)
          .alignItems(VerticalAlign.Center)
          .padding({ bottom: 20 })
          .border({ width: { bottom: 1 }, color: '#ccc' })
        }
        .hideTitleBar(false)
        .titleMode(NavigationTitleMode.Mini)
        .title('个人中心设置')
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
  • 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.
//CustomKeyboard.ets
import { CommonPage } from './CommonPage';

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(() => {
              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
  bindSheetBuilder() {
    Column() {
      Text('填写手机短信验证码')
        .fontWeight(FontWeight.Bold)
        .margin({ top: 20, bottom: 20 })
      Row() {
        Text() {
          Span('已发送至')
            .fontSize(12)
            .fontWeight(400)
            .fontColor(Color.Grey)
        }

        Blank().width(5)
        Text() {
          Span('156****5163')
            .fontSize(12)
            .fontWeight(500)
            .fontColor(Color.Black)
        }
      }.width('100%')
      .justifyContent(FlexAlign.Start)
      .padding({ left: 20, right: 20 })
      .margin({ bottom: 20 })

      // 展示验证码
      this.buildAEnterCodeInput()
    }
  }

  // 公共页面,点击即可拉起半模态
  @Builder
  buildTitleName() {
    CommonPage()
      .onClick(() => {
        this.isShow = true
      })
      .bindSheet($$this.isShow, this.bindSheetBuilder, {
        height: '70%',
        blurStyle: BlurStyle.Thick,
        showClose: false,
        preferType: SheetType.CENTER
      })

  }

  @Builder
  componentBuilder() {
    Column() {
      this.buildTitleName()
      Blank().height(38)
      this.buildAEnterCodeInput()
      Blank().height(20)
    }
    .margin({ top: 10 })
    .borderRadius(8)
    .backgroundColor(Color.White)
  }

  // 验证码的样式
  @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(() => {
          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() {
      this.componentBuilder()
    }
  }
}
  • 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.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
//SystemKeyboard.ets
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */
import inputMethod from '@ohos.inputMethod';
import { BusinessError } from '@ohos.base';
import { CommonPage } from './CommonPage'

@Entry
@Component
struct SystemKeyboard {
  // 输入的验证码
  @State codeTxt: string = '';
  // 初始化屏幕宽度
  @State screenWidth: number = 0;
  // 模态窗口是否展示
  @State isShow: boolean = false
  private isAttached: boolean = false;
  private inputController: inputMethod.InputMethodController = inputMethod.getController();
  private keyboardStatus: number = 0; // 默认是none, 1是hide,2是show

  aboutToAppear(): void {
    console.info('IsaKit aboutToAppear')
    this.screenWidth = 1080;
  }

  @Builder
  componentBuilder() {
    Column() {
      this.buildTitleName()
      Blank().height(38)
      this.buildAEnterCodeInput()
      Blank().height(20)
    }
    .margin({ top: 10 })
    .borderRadius(8)
    .backgroundColor(Color.White)
  }
  // 模态弹窗样式
  @Builder
  bindSheetBuilder() {
    Column() {
      Text('填写手机短信验证码')
        .fontWeight(FontWeight.Bold)
        .margin({ top: 20, bottom: 20 })
      Row() {
        Text() {
          Span('已发送至')
            .fontSize(12)
            .fontWeight(400)
            .fontColor(Color.Grey)
        }

        Blank().width(5)
        Text() {
          Span('156****5163')
            .fontSize(12)
            .fontWeight(500)
            .fontColor(Color.Black)
        }
      }.width('100%')
      .justifyContent(FlexAlign.Start)
      .padding({ left: 20, right: 20 })
      .margin({ bottom: 20 })
      this.buildAEnterCodeInput()
    }
  }
  // 拉起模态弹窗
  @Builder
  buildTitleName() {
    CommonPage()
      .onClick(() => {
        this.isShow = true
      })
      .bindSheet($$this.isShow, this.bindSheetBuilder, {
        height: '70%',
        blurStyle: BlurStyle.Thick,
        showClose: false,
        preferType: SheetType.CENTER
      })
  }

  // 单个验证码的格式
  @Styles
  fancyUse(){
    .width((this.screenWidth - 110) / 6)
    .height('100%')
    .margin({ left: 5, right: 5 })
    .border({
      width: 0.5,
      color: Color.Grey,
      style: BorderStyle.Solid
    })
    .borderRadius(5)
  }

  @Builder
  buildAEnterCodeInput() {
    Flex({
      direction: FlexDirection.Row,
      alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.SpaceBetween
    }) {
      Text(this.codeTxt[0]) {
      }
      .fontSize(18)
      .fontWeight(600)
      .textAlign(TextAlign.Center)
      .fancyUse()

      Text(this.codeTxt[1])
        .fontSize(18)
        .fontWeight(600)
        .textAlign(TextAlign.Center)
        .fancyUse()

      Text(this.codeTxt[2])
        .fontSize(18)
        .fontWeight(600)
        .textAlign(TextAlign.Center)
        .fancyUse()

      Text(this.codeTxt[3])
        .fontSize(18)
        .fontWeight(600)
        .textAlign(TextAlign.Center)
        .fancyUse()

      Text(this.codeTxt[4])
        .fontSize(18)
        .fontWeight(600)
        .textAlign(TextAlign.Center)
        .fancyUse()

      Text(this.codeTxt[5])
        .fontSize(18)
        .fontWeight(600)
        .textAlign(TextAlign.Center)
        .fancyUse()

    }
    .backgroundColor(Color.White)
    .height(50)
    .margin({ left: 15, right: 15 })
    .id('customInput')
    .defaultFocus(false)
    .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
      console.info('IsaKit Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
      if (isVisible && currentRatio >= 1.0) {
        console.info('IsaKit Test Text is fully visible. currentRatio:' + currentRatio)
        this.attachAndListener();
      }

      if (!isVisible && currentRatio <= 0.0) {
        console.info('IsaKit Test Text is completely invisible.')
        this.detach()
      }
    })
    .onClick(async () => {
      console.info('IsaKit keyboardStatus =' + this.keyboardStatus)
      if (this.isAttached && this.keyboardStatus != 2) {
        // 输入法配置项
        let textConfig: inputMethod.TextConfig = {
          inputAttribute: {
            textInputType: inputMethod.TextInputType.NUMBER,
            enterKeyType: inputMethod.EnterKeyType.GO
          }
        };
        // 控件绑定输入法
        await this.inputController.attach(true, textConfig)
        return
      }
    })
  }
  // 解绑输入法
  detach() {
    this.inputController.off('insertText');
    this.inputController.off('deleteLeft');
    this.inputController.off('sendKeyboardStatus');
    this.inputController.detach((err: BusinessError) => {
      if (err) {
        console.error(`Failed to detach: ${JSON.stringify(err)}`);
        return;
      }
      this.isAttached = false
      console.log('Succeeded in detaching inputMethod.');
    });
  }

  // 绑定和设置监听
  async attachAndListener() {
    // 输入法配置项
    let textConfig: inputMethod.TextConfig = {
      inputAttribute: {
        textInputType: inputMethod.TextInputType.NUMBER,
        enterKeyType: inputMethod.EnterKeyType.GO
      }
    };

    // 控件绑定输入法
    await this.inputController.attach(true, textConfig)
    this.isAttached = true
    this.attachListener()
  }

  /*
   * 订阅输入法回调
   */
  attachListener(): void {
    // 订阅输入法应用插入文本事件
    this.inputController.on('insertText', (text) => {
      if (this.codeTxt.length >= 6) {
        return
      }
      this.codeTxt += text;
      console.info('this.inputText', 'insertText this.inputText===' + this.codeTxt)
    })

    // 订阅输入法应用向左删除事件
    this.inputController.on('deleteLeft', (length) => {
      this.codeTxt = this.codeTxt.substring(0, this.codeTxt.length - 1);
      console.info('this.inputText', 'deleteLeft  this.inputText===' + this.codeTxt, 'length' + length)
    })

    // 订阅输入法应用发送输入法软键盘状态事件
    this.inputController.on('sendKeyboardStatus', (keyboardStatus: inputMethod.KeyboardStatus) => {
      this.keyboardStatus = keyboardStatus
      console.info('IsaKit this.inputText keyboardStatus= ' + this.keyboardStatus)
    })
  }

  build() {
    Column() {
      this.componentBuilder()
    }
  }
}
  • 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.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
分享
微博
QQ
微信
回复
2024-12-24 18:54:49
相关问题
HarmonyOS 验证码输入样式
806浏览 • 1回复 待解决
如何实现一个验证码弹窗子窗口
1211浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
1295浏览 • 1回复 待解决
实现验证码登录之前滑动验证实现
798浏览 • 1回复 待解决
前端验证码配合后端实现思路?
4146浏览 • 1回复 待解决
HarmonyOS 申请验证码demo实现
820浏览 • 1回复 待解决
数据库一个设计问题?
3009浏览 • 1回复 待解决