W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
圖案密碼鎖組件,以九宮格圖案的方式輸入密碼,用于密碼驗(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ù)類型 | 必填 | 描述 |
---|---|---|---|
controller | PatternLockController | 否 | 設(shè)置PatternLock組件控制器,可用于控制組件狀態(tài)重置。 |
不支持除backgroundColor以外的通用屬性設(shè)置。
名稱 | 參數(shù)類型 | 描述 |
---|---|---|
sideLength | Length | 設(shè)置組件的寬度和高度(寬高相同)。設(shè)置為0或負(fù)數(shù)等非法值時(shí)組件不顯示。 默認(rèn)值:300vp |
circleRadius | Length | 設(shè)置宮格中圓點(diǎn)的半徑。 默認(rèn)值:14vp |
regularColor | ResourceColor | 設(shè)置宮格圓點(diǎn)在“未選中”狀態(tài)的填充顏色。 默認(rèn)值:Color.Black |
selectedColor | ResourceColor | 設(shè)置宮格圓點(diǎn)在“選中”狀態(tài)的填充顏色。 默認(rèn)值:Color.Black |
activeColor | ResourceColor | 設(shè)置宮格圓點(diǎn)在“激活”狀態(tài)的填充顏色(“激活”狀態(tài)為手指經(jīng)過圓點(diǎn)但還未選中的狀態(tài))。 默認(rèn)值:Color.Black |
pathColor | ResourceColor | 設(shè)置連線的顏色。 默認(rèn)值:Color.Blue |
pathStrokeWidth | number | string | 設(shè)置連線的寬度。設(shè)置為0或負(fù)數(shù)等非法值時(shí)連線不顯示。 默認(rèn)值:34vp |
autoReset | boolean | 設(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)。 |
- // xxx.ets
- @Entry
- @Component
- struct PatternLockExample {
- @State passwords: Number[] = []
- @State message: string = 'please input password!'
- private patternLockController: PatternLockController = new PatternLockController()
- build() {
- Column() {
- Text(this.message).textAlign(TextAlign.Center).margin(20).fontSize(20)
- PatternLock(this.patternLockController)
- .sideLength(200)
- .circleRadius(9)
- .pathStrokeWidth(18)
- .activeColor('#B0C4DE')
- .selectedColor('#228B22')
- .pathColor('#90EE90')
- .backgroundColor('#F5F5F5')
- .autoReset(true)
- .onPatternComplete((input: Array<number>) => {
- // 輸入的密碼長度小于5時(shí),提示重新輸入
- if (input === null || input === undefined || input.length < 5) {
- this.message = 'The password length needs to be greater than 5, please enter again.'
- return
- }
- // 判斷密碼長度是否大于0
- if (this.passwords.length > 0) {
- // 判斷兩次輸入的密碼是否相同,相同則提示密碼設(shè)置成功,否則提示重新輸入
- if (this.passwords.toString() === input.toString()) {
- this.passwords = input
- this.message = 'Set password successfully: ' + this.passwords.toString()
- } else {
- this.message = 'Inconsistent passwords, please enter again.'
- }
- } else {
- // 提示第二次輸入密碼
- this.passwords = input
- this.message = "Please enter again."
- }
- })
- Button('Reset PatternLock').margin(30).onClick(() => {
- // 重置密碼鎖
- this.patternLockController.reset()
- this.passwords = []
- this.message = 'Please input password'
- })
- }.width('100%').height('100%')
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: