要求:
- 英文大写字符(A~Z)
- 英文小写字符(a~z)
- 数字(0~9)
- 除了英文句号特殊符号“.”,其他的不能使用
效果图:
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 }
activity_test05.xml代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".test02.Test05Activity"> 9 <LinearLayout 10 android:layout_marginLeft="10dp" 11 android:layout_marginRight="10dp" 12 android:orientation="horizontal" 13 android:layout_width="match_parent" 14 android:layout_height="45dp"> 15 <EditText 16 android:id="@+id/passwordEdit" 17 android:hint="请输入密码" 18 android:singleLine="true" 19 android:maxLength="16" 20 android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ." 21 android:layout_weight="1" 22 android:layout_width="0dp" 23 android:layout_marginRight="@dimen/dp_10" 24 android:layout_height="match_parent"/> 25 <Button 26 android:id="@+id/btnClean" 27 android:text="清除" 28 android:layout_width="80dp" 29 android:layout_height="match_parent"/> 30 </LinearLayout> 31 <LinearLayout 32 android:visibility="gone" 33 android:id="@+id/passwordLayout" 34 android:layout_marginRight="10dp" 35 android:layout_marginLeft="10dp" 36 android:gravity="center_vertical" 37 android:orientation="horizontal" 38 android:layout_width="match_parent" 39 android:layout_height="45dp"> 40 <TextView 41 android:text="密码强度:" 42 android:textColor="#333" 43 android:textSize="16sp" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content"/> 46 <View 47 android:background="#E03D06" 48 android:layout_width="40dp" 49 android:layout_height="15dp"/> 50 <View 51 android:visibility="gone" 52 android:id="@+id/passwordView1" 53 android:layout_marginLeft="5dp" 54 android:background="#F2831A" 55 android:layout_width="40dp" 56 android:layout_height="15dp"/> 57 <View 58 android:visibility="gone" 59 android:id="@+id/passwordView2" 60 android:layout_marginLeft="5dp" 61 android:background="#6A9F09" 62 android:layout_width="40dp" 63 android:layout_height="15dp"/> 64 </LinearLayout> 65 </LinearLayout>