#鸿蒙通关秘籍#完整的验证码输入组件的实现方式是什么?

HarmonyOS
2024-11-28 14:44:10
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
DataDevil

完整的验证码输入组件实现需要包括以下内容:组件参数定义、自绘控件绑定和事件解绑、输入法事件回调设置以及组件布局。

import { inputMethod } from '@kit.IMEKit';

@Component
export default struct VerifyCodeInput {
  // 参数定义
  @Prop iheight:number|string=48
  @Prop iwidth:number|string=48
  @Prop fontsize:number|string='20fp'
  @Prop count:number=6
  private inputController: inputMethod.InputMethodController = inputMethod.getController();
  private textConfig: inputMethod.TextConfig = {
    inputAttribute: {
      textInputType: inputMethod.TextInputType.NUMBER, // 只能输入数字
      enterKeyType: inputMethod.EnterKeyType.GO
    },
  };
  @Link inputCode: string
  @State countArr: number[] = new Array(this.count??6).fill(' ')
  private isFirst:boolean=true

  async onAttachInput(isFocus?:boolean) {
    try { await this.inputController.attach(isFocus??true, this.textConfig); }
    catch (e) { console.log(JSON.stringify(e)) }
  }

  async onDetachInput() {
    try { await this.inputController.detach() }
    catch (e) { console.log(JSON.stringify(e)) }
  }

  onInputEvent() {
    this.inputController.on("insertText", (text: string) => {
      if (text && this.inputCode.length < this.count && !isNaN(Number(text))) {
        this.inputCode += text;
      }
    })
    this.inputController.on("deleteLeft", (length: number) => {
      this.inputCode = this.inputCode.substring(0, this.inputCode.length - 1);
    })
  }

  offInputEvent(){
    this.inputController.off("insertText");
    this.inputController.off("deleteLeft");
  }

  aboutToDisappear() {
    this.offInputEvent()
    this.onDetachInput()
  }

  build() {
    Column() {
      Row() {
        // 布局和事件代码省略...
      }
    }.width('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.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
分享
微博
QQ
微信
回复
2024-11-28 16:13:45


相关问题
HarmonyOS 验证码输入样式
794浏览 • 1回复 待解决
密码或验证码登录页面完整代码
3492浏览 • 1回复 待解决
实现验证码登录之前滑动验证实现
773浏览 • 1回复 待解决
前端验证码配合后端实现思路?
4118浏览 • 1回复 待解决
HarmonyOS 申请验证码demo实现
799浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
1259浏览 • 1回复 待解决
如何实现一个验证码弹窗子窗口
1188浏览 • 1回复 待解决