androidstudio通过监听EDITTEXT密码动态输入并且通过progressbar实现密码强度的显示条

androidstudio通过监听EDITTEXT密码动态输入并且通过progressbar实现密码强度的显示条

一、xml文件

xml文件:(我目前使用的是LinearLayout)

	
    
    

androidstudio通过监听EDITTEXT密码动态输入并且通过progressbar实现密码强度的显示条_第1张图片

二、java文件

java文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enroll);
    EditText enrollpassword1 = (EditText) findViewById(R.id.enrollpassword1);
    ProgressBar pwdstrlevel = findViewById(R.id.progressbar);
    enrollpassword1.addTextChangedListener(new TextWatcher() {
    
	@Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			Log.d("TAG","beforeTextChanged--------------->");
		}

	@Override
        public void afterTextChanged(Editable s) {
			Log.d("TAG","afterTextChanged--------------->");
        }

	@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
			Log.d("TAG","onTextChanged--------------->");
            String str =enrollpassword1.getText().toString();
            if(str.matches(("^[a-zA-Z]\\d{5,10}$")) ){
				pwdstrlevel.setProgress(20);
                Toast.makeText(enroll.this,"密码强度:很弱",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z]\\D{5,10}$")){
				pwdstrlevel.setProgress(20);
			    Toast.makeText(enroll.this,"密码强度:很弱",Toast.LENGTH_SHORT).show();
			 }

			else if(str.matches("[a-zA-Z]{6,11}$")){
				pwdstrlevel.setProgress(20);
                Toast.makeText(enroll.this,"密码强度:很弱",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z]\\W{5,10}$")){
				pwdstrlevel.setProgress(35);
                Toast.makeText(enroll.this,"密码强度:中下",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z][\\d\\W]{5,10}$")){
				pwdstrlevel.setProgress(50);
                Toast.makeText(enroll.this,"密码强度:中",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z][\\D\\W]{5,10}$")){
				pwdstrlevel.setProgress(50);
                Toast.makeText(enroll.this,"密码强度:中",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z]\\w{5,10}$")){			
				pwdstrlevel.setProgress(35);
                Toast.makeText(enroll.this,"密码强度:中下",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z][\\w\\W]{5,10}$")){		
				pwdstrlevel.setProgress(50);
                Toast.makeText(enroll.this,"密码强度:中",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches(("^[a-zA-Z]\\d{11,}$")) ){		
				pwdstrlevel.setProgress(50);
                Toast.makeText(enroll.this,"密码强度:中",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z]\\D{11,}$")){			
				pwdstrlevel.setProgress(50);
                Toast.makeText(enroll.this,"密码强度:中",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("[a-zA-Z]{12,}$")){		
				pwdstrlevel.setProgress(75);
                Toast.makeText(enroll.this,"密码强度:中上",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z]\\W{11,}$")){			
				pwdstrlevel.setProgress(75);
                Toast.makeText(enroll.this,"密码强度:中上",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z][\\d\\W]{11,}$")){		
				pwdstrlevel.setProgress(75);
                Toast.makeText(enroll.this,"密码强度:中上",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z][\\D\\W]{11,}$")){		
				pwdstrlevel.setProgress(75);
                Toast.makeText(enroll.this,"密码强度:中上",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z]\\w{11,}$")){		
				pwdstrlevel.setProgress(75);
                Toast.makeText(enroll.this,"密码强度:中上",Toast.LENGTH_SHORT).show();
            }

			else if(str.matches("^[a-zA-Z][\\w\\W]{11,}$")){		
				pwdstrlevel.setProgress(100);
                Toast.makeText(enroll.this,"密码强度:强",Toast.LENGTH_SHORT).show();
            }

			else{	
				pwdstrlevel.setProgress(0);
                Toast.makeText(enroll.this,"密码格式错误",Toast.LENGTH_SHORT).show();
            }

}

});

}

总结

live long and prosper

你可能感兴趣的:(androidstudio,progressbar,edittext,android,studio,progressbar)