圖案密碼鎖組件

2024-01-22 17:22 更新

圖案密碼鎖組件,以九宮格圖案的方式輸入密碼,用于密碼驗(yàn)證場(chǎng)景。手指在PatternLock組件區(qū)域按下時(shí)開始進(jìn)入輸入狀態(tài),手指離開屏幕時(shí)結(jié)束輸入狀態(tài)完成密碼輸入。

說明

該組件從API Version 9開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

子組件

接口

PatternLock(controller?: PatternLockController)

參數(shù):

參數(shù)名參數(shù)類型必填描述
controllerPatternLockController設(shè)置PatternLock組件控制器,可用于控制組件狀態(tài)重置。

屬性

不支持除backgroundColor以外的通用屬性設(shè)置。

名稱參數(shù)類型描述
sideLengthLength

設(shè)置組件的寬度和高度(寬高相同)。設(shè)置為0或負(fù)數(shù)等非法值時(shí)組件不顯示。

默認(rèn)值:300vp

circleRadiusLength

設(shè)置宮格中圓點(diǎn)的半徑。

默認(rèn)值:14vp

regularColorResourceColor

設(shè)置宮格圓點(diǎn)在“未選中”狀態(tài)的填充顏色。

默認(rèn)值:Color.Black

selectedColorResourceColor

設(shè)置宮格圓點(diǎn)在“選中”狀態(tài)的填充顏色。

默認(rèn)值:Color.Black

activeColorResourceColor

設(shè)置宮格圓點(diǎn)在“激活”狀態(tài)的填充顏色(“激活”狀態(tài)為手指經(jīng)過圓點(diǎn)但還未選中的狀態(tài))。

默認(rèn)值:Color.Black

pathColorResourceColor

設(shè)置連線的顏色。

默認(rèn)值:Color.Blue

pathStrokeWidthnumber | string

設(shè)置連線的寬度。設(shè)置為0或負(fù)數(shù)等非法值時(shí)連線不顯示。

默認(rèn)值:34vp

autoResetboolean

設(shè)置在完成密碼輸入后再次在組件區(qū)域按下時(shí)是否重置組件狀態(tài)。設(shè)置為true,完成密碼輸入后再次在組件區(qū)域按下時(shí)會(huì)重置組件狀態(tài)(即清除之前輸入的密碼);反之若設(shè)置為false,則不會(huì)重置組件狀態(tài)。

默認(rèn)值:true

事件

除支持通用事件外,還支持以下事件:

名稱描述
onPatternComplete(callback: (input: Array<number>) => void)

密碼輸入結(jié)束時(shí)觸發(fā)該回調(diào)。

input: 與選中宮格圓點(diǎn)順序一致的數(shù)字?jǐn)?shù)組,數(shù)字為選中宮格圓點(diǎn)的索引值(第一行圓點(diǎn)從左往右依次為0,1,2,第二行圓點(diǎn)依次為3,4,5,第三行圓點(diǎn)依次為6,7,8)。

PatternLockController

PatternLock組件的控制器,可以通過它進(jìn)行組件狀態(tài)重置。

導(dǎo)入對(duì)象

  1. patternLockController: PatternLockController = new PatternLockController()

reset

reset(): void

重置組件狀態(tài)。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct PatternLockExample {
  5. @State passwords: Number[] = []
  6. @State message: string = 'please input password!'
  7. private patternLockController: PatternLockController = new PatternLockController()
  8. build() {
  9. Column() {
  10. Text(this.message).textAlign(TextAlign.Center).margin(20).fontSize(20)
  11. PatternLock(this.patternLockController)
  12. .sideLength(200)
  13. .circleRadius(9)
  14. .pathStrokeWidth(18)
  15. .activeColor('#B0C4DE')
  16. .selectedColor('#228B22')
  17. .pathColor('#90EE90')
  18. .backgroundColor('#F5F5F5')
  19. .autoReset(true)
  20. .onPatternComplete((input: Array<number>) => {
  21. // 輸入的密碼長度小于5時(shí),提示重新輸入
  22. if (input === null || input === undefined || input.length < 5) {
  23. this.message = 'The password length needs to be greater than 5, please enter again.'
  24. return
  25. }
  26. // 判斷密碼長度是否大于0
  27. if (this.passwords.length > 0) {
  28. // 判斷兩次輸入的密碼是否相同,相同則提示密碼設(shè)置成功,否則提示重新輸入
  29. if (this.passwords.toString() === input.toString()) {
  30. this.passwords = input
  31. this.message = 'Set password successfully: ' + this.passwords.toString()
  32. } else {
  33. this.message = 'Inconsistent passwords, please enter again.'
  34. }
  35. } else {
  36. // 提示第二次輸入密碼
  37. this.passwords = input
  38. this.message = "Please enter again."
  39. }
  40. })
  41. Button('Reset PatternLock').margin(30).onClick(() => {
  42. // 重置密碼鎖
  43. this.patternLockController.reset()
  44. this.passwords = []
  45. this.message = 'Please input password'
  46. })
  47. }.width('100%').height('100%')
  48. }
  49. }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)