• Home
Name Date Size #Lines LOC

..--

AppScope/22-Oct-2025-3532

casesfeature/verifycode/22-Oct-2025-517466

entry/22-Oct-2025-535482

hvigor/22-Oct-2025-3836

.gitignoreD22-Oct-2025133 1212

README.mdD22-Oct-20253.2 KiB12694

build-profile.json5D22-Oct-20251.4 KiB6160

code-linter.json5D22-Oct-20251.4 KiB4746

hvigorfile.tsD22-Oct-2025843 225

oh-package.json5D22-Oct-2025809 2624

ohosTest.mdD22-Oct-2025758 97

README.md

1# 验证码布局
2
3### 介绍
4
5本示例介绍如何使用Text组件实现验证码场景,并禁用对内容的选中、复制、光标。
6
7### 效果图预览
8
9<img src="casesfeature/verifycode/src/main/resources/base/media/verify_code.gif" width="300">
10
11**使用说明**
12
131. 单击组件可弹出输入法
142. 在进行验证码输入时,无法对中间单个数字进行更改,无法选中输入内容,无光标
15
16### 下载安装
17
181.模块oh-package.json5文件中引入依赖。
19
20```typescript
21"dependencies":
22{
23  "verifycode":
24  "har包地址"
25}
26```
27
282.ets文件import列表视图组件。
29
30```typescript
31import { VerifyCodeViewComponent } from 'verifycode';
32```
33
34### 实现思路
35
361. 因为要禁用复制、选中等功能,这里使用了Text组件,而不是TextInput
37
38    ```typescript
39    ForEach(this.codeIndexArray, (item: number, index: number) => {
40      Text(this.codeText[item])
41        .verifyCodeUnitStyle()
42    }, (item: number, index: number) => item.toString())
43    ```
44
451. 绑定输入法,并默认显示键盘
46    ```typescript
47    this.inputController.attach(true, textConfig);
48    ```
49
501. 订阅输入法插入、删除事件,从而获取输入内容
51     ```typescript
52     this.inputController.on("insertText", (text: string) => {
53       if (this.codeText.length >= this.verifyCodeLength) {
54         return;
55       }
56         this.codeText += text;
57     })
58     this.inputController.on("deleteLeft", (length: number) => {
59       this.codeText = this.codeText.substring(0, this.codeText.length - 1);
60     })
61     ```
621. 由于这里使用的是Text组件,而非TextInput组件,因此需要每次点击目标的组件的时候都重新绑定,并设置键盘的显示,而不能直接使用showSoftKeyboard
63   ```typescript
64   Flex(){
65      //...
66   }.onClick(() => {
67      this.attachAndListen();
68   })
69   ```
701. 当组件失焦的时候解除监听
71   ```typescript
72    off(): void {
73      this.inputController.off("insertText");
74      this.inputController.off("deleteLeft");
75      this.isListen = false;
76      logger.info('detached');
77    }
78
79    Flex(){
80      //...
81    }
82    .onBlur(() => {
83      this.off();
84    })
85   ```
86
87### 高性能知识点
88
89**不涉及**
90
91### 工程结构&模块类型
92
93   ```
94   verifycode                                       // har类型
95   |---constants
96   |   |---VerifyCodeConstants.ets                  // 常量
97   |---view
98   |   |---VerifyCodeView.ets                       // 视图层-验证码组件
99   ```
100
101### 参考资料
102
1031. [Text](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/reference/apis-arkui/arkui-js/js-components-basic-text.md)
1042. [inputMethod](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/reference/apis-ime-kit/js-apis-inputmethod.md)
105
106### 约束于限制
107
1081.本示例仅支持标准系统上运行。
109
1102.本示例已适配API version 12版本SDK。
111
1123.本示例需要使用DevEco Studio 5.0.0 Release及以上版本才可编译运行
113
114### 下载
115
116如需单独下载本工程,执行如下命令:
117
118  ```
119  git init
120  git config core.sparsecheckout true
121  echo /code/UI/VerifyCode/ > .git/info/sparse-checkout
122  git remote add origin https://gitee.com/openharmony/applications_app_samples.git
123  git pull origin master
124  ```
125
126