快應用如何實現密碼明文和密文切換顯示


很多應用提供了賬號登錄、注冊功能,在輸入密碼時,開發者為了安全性,當用戶輸入密碼時,一般都顯示……的密文。但是,這個體驗也給用戶造成了不便,用戶不知道當前輸入的字符是否是自己期望的,也無法知道當前輸入到哪個字符。針對這個問題,開發者進行了優化,在輸入框旁邊提供了小圖標,點擊可切換顯示明文和密文。快應用開發也是可以實現上述功能的。

解決方案

  1. 密碼輸入框使用input組件,input組件提供了多種type值,密文使用type類型為password,明文類型可使用text類型,type字段需要綁定動態變量。

  2. 在明文和密文切換時,需要顯示設置光標位置在末尾,要不切換后光標會顯示在開始位置。設置光標位置可以通過setSelectionRange()方法,方法中的start和end參數都設置成當前輸入的文字長度。

示例代碼如下:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
< template >
   < div  class = "container" >
     < stack  class = "input-item"
       < input  class = "input-text"  type = "{{inputtype}}"  id = "inputpsdID"  placeholder = "please enter password"  onchange = "showChangePrompt" ></ input >
       < image  src = "../Common/lock.png"   class = "lock"  onclick = "switchpassandshow" ></ image >
     </ stack >
   </ div >
</ template >
  
< style >
   .container {
     flex: 1;
     padding: 20px;
     flex-direction: column;
     align-items: center;
   }
  
   .input-item {
     margin-bottom: 80px;
     margin-top: 10px;
     margin-left: 16px;
     margin-right: 16px;
      align-items: center;
      justify-content: flex-end; 
   }
  
   .lock{
      width: 40px;
      height:40px;   
   }
  
   .input-text {
     height: 80px;
     width: 100%;
     line-height: 80px;
     border-top-width: 1px;
     border-bottom-width: 1px;
     border-color: #999999;
     font-size: 30px;
    
     padding-right: 42px;
   }
  
   .input-text:focus {
     border-color: #f76160;
   }
</ style >
  
< script >
   export default {
     data: {
       inputtype: 'password',
       lenth: 0
     },
     onInit() {
       this.$page.setTitleBar({ text: 'Switching between plaintext and ciphertext' });
     },
  
     showChangePrompt(e) {
       this.lenth = e.value.length;
       console.info("showChangePrompt   this.lenth= " + this.lenth);
  
     },
  
     switchpassandshow: function () {
       var com = this.$element('inputpsdID');
       if (this.inputtype === 'password') {
         this.inputtype = 'text';
       } else {
         this.inputtype = 'password';
       }
        com.setSelectionRange({ start: this.lenth, end: this.lenth});
     }
   }
</ script >

 

上述代碼實現了一個簡易的密碼明文和密文切換顯示功能,點擊右邊的鎖圖標,可以切換顯示明文和密文。效果如下圖所示:

                    

 

 

 

 

 

 

 

 

 

 
 

圖1  密碼明文顯示

 
 

圖2 密碼密文顯示

 

 

欲了解更多詳情,請參見:

快應用input組件開發指導:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-component-input#h1-1575544278909

 

原文鏈接:developer.huawei.com/consumer/cn…

原作者:Mayism


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM