要求:

  • 英文大写字符(A~Z)
  • 英文小写字符(a~z)
  • 数字(0~9)
  • 除了英文句号特殊符号“.”,其他的不能使用

效果图:

android 密码强度校验

 MainActivity代码:

 1 public class Test05Activity extends AppCompatActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_test05);
 7         initView();
 8     }
 9 
10     private void initView(){
11         EditText passwordEdit = findViewById(R.id.passwordEdit);
12         Button btnClean = findViewById(R.id.btnClean);
13         LinearLayout passLayout = findViewById(R.id.passwordLayout);
14         View passView1 = findViewById(R.id.passwordView1);
15         View passView2 = findViewById(R.id.passwordView2);
16         btnClean.setOnClickListener(new View.OnClickListener() {
17             @Override
18             public void onClick(View v) {
19                 passwordEdit.setText(null);
20             }
21         });
22         passwordEdit.addTextChangedListener(new TextWatcher() {
23             @Override
24             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
25 
26             }
27 
28             @Override
29             public void onTextChanged(CharSequence s, int start, int before, int count) {
30 
31             }
32 
33             @Override
34             public void afterTextChanged(Editable s) {
35                 if(s.toString().length() > 0){
36                     if(s.toString().length() == 1)
37                         passLayout.setVisibility(View.VISIBLE);
38                     switch (checkPassWord(s.toString())){
39                         case 1:
40                             passView1.setVisibility(View.GONE);
41                             passView2.setVisibility(View.GONE);
42                             break;
43                         case 2:
44                             passView1.setVisibility(View.VISIBLE);
45                             passView2.setVisibility(View.GONE);
46                             break;
47                         case 3:
48                             passView2.setVisibility(View.VISIBLE);
49                             break;
50                     }
51                 }else{
52                     passLayout.setVisibility(View.GONE);
53                 }
54             }
55         });
56     }
57 
58     private int checkPassWord(String passwordStr){
59         String regexZ = "\\d*";
60         String regexS = "[a-zA-Z]+";
61         String regexT = "\\W+$";
62         String regexZT = "\\D*";
63         String regexST = "[\\d\\W]*";
64         String regexZS = "\\w*";
65         String regexZST = "[\\w\\W]*";
66 
67         int pass = 0;
68         if (passwordStr.matches(regexZ)) {
69             return 1;
70         }
71         if (passwordStr.matches(regexS)) {
72             return 1;
73         }
74         if (passwordStr.matches(regexT)) {
75             return 1;
76         }
77         if (passwordStr.matches(regexZT)) {
78             return 2;
79         }
80         if (passwordStr.matches(regexST)) {
81             return 2;
82         }
83         if (passwordStr.matches(regexZS)) {
84             return 2;
85         }
86         if (passwordStr.matches(regexZST)) {
87             return 3;
88         }
89         return pass;
90     }
91 }
MainActivity

相关文章:

  • 2021-11-01
  • 2022-12-23
  • 2021-09-02
  • 2022-02-11
  • 2021-12-03
  • 2021-10-23
  • 2021-09-27
  • 2022-12-23
猜你喜欢
  • 2022-03-07
  • 2021-10-03
  • 2021-08-06
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案