145 Star 1.4K Fork 1.7K

OpenHarmony/applications_app_samples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

验证码布局

介绍

本示例介绍如何使用Text组件实现验证码场景,并禁用对内容的选中、复制、光标。

效果图预览

使用说明

  1. 单击组件可弹出输入法
  2. 在进行验证码输入时,无法对中间单个数字进行更改,无法选中输入内容,无光标

下载安装

1.模块oh-package.json5文件中引入依赖。

"dependencies":
{
  "verifycode":
  "har包地址"
}

2.ets文件import列表视图组件。

import { VerifyCodeViewComponent } from 'verifycode';

实现思路

  1. 因为要禁用复制、选中等功能,这里使用了Text组件,而不是TextInput

    ForEach(this.codeIndexArray, (item: number, index: number) => {
      Text(this.codeText[item])
        .verifyCodeUnitStyle()
    }, (item: number, index: number) => item.toString())
    
  2. 绑定输入法,并默认显示键盘

    this.inputController.attach(true, textConfig);
    
  3. 订阅输入法插入、删除事件,从而获取输入内容

    this.inputController.on("insertText", (text: string) => {
      if (this.codeText.length >= this.verifyCodeLength) {
        return;
      }
        this.codeText += text;
    })
    this.inputController.on("deleteLeft", (length: number) => {
      this.codeText = this.codeText.substring(0, this.codeText.length - 1);
    })
    
  4. 由于这里使用的是Text组件,而非TextInput组件,因此需要每次点击目标的组件的时候都重新绑定,并设置键盘的显示,而不能直接使用showSoftKeyboard

    Flex(){
       //...
    }.onClick(() => {
       this.attachAndListen();
    })
    
  5. 当组件失焦的时候解除监听

     off(): void {
       this.inputController.off("insertText");
       this.inputController.off("deleteLeft");
       this.isListen = false;
       logger.info('detached');
     }
    
     Flex(){
       //...
     }
     .onBlur(() => {
       this.off();
     })
    

高性能知识点

不涉及

工程结构&模块类型

verifycode                                       // har类型
|---constants
|   |---VerifyCodeConstants.ets                  // 常量
|---view
|   |---VerifyCodeView.ets                       // 视图层-验证码组件

参考资料

  1. Text
  2. inputMethod

约束于限制

1.本示例仅支持标准系统上运行。

2.本示例已适配API version 12版本SDK。

3.本示例需要使用DevEco Studio 5.0.0 Release及以上版本才可编译运行

下载

如需单独下载本工程,执行如下命令:

git init
git config core.sparsecheckout true
echo /code/UI/VerifyCode/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openharmony/applications_app_samples.git
git@gitee.com:openharmony/applications_app_samples.git
openharmony
applications_app_samples
applications_app_samples
master

搜索帮助