上一篇文章说根据密码判断强度:原文:http://blog.csdn.net/luckrr/article/details/52762677
闲来无事又多加了个进度条显示的小功能,
代码如下:
/**
* 判断密码强度
*
* @return Z = 字母 S = 数字 T = 特殊字符
*/
private String passwordStrong(String passwordStr) {
if (TextUtils.equals("", passwordStr)) {
return "出现故障";
}
String regexZ = "\\d*";
String regexS = "[a-zA-Z]+";
String regexT = "\\W+$";
String regexZT = "\\D*";
String regexST = "[\\d\\W]*";
String regexZS = "\\w*";
String regexZST = "[\\w\\W]*";
if (passwordStr.matches(regexZ)) {
GRADE_SCORE = 20;
return "弱";
}
if (passwordStr.matches(regexS)) {
GRADE_SCORE = 20;
return "弱";
}
if (passwordStr.matches(regexT)) {
GRADE_SCORE = 20;
return "弱";
}
if (passwordStr.matches(regexZT)) {
GRADE_SCORE = 60;
return "中";
}
if (passwordStr.matches(regexST)) {
GRADE_SCORE = 60;
return "中";
}
if (passwordStr.matches(regexZS)) {
GRADE_SCORE = 60;
return "中";
}
if (passwordStr.matches(regexZST)) {
GRADE_SCORE = 90;
return "强";
}
return passwordStr;
}
为progressbar设置进度值;
/**
* 设置progressBar值
*
* @param score
*/
private void setProgressBarColour(int score) {
int color = 0;
if (score < 30) {
color = getResources().getColor(R.color.bg_red);
} else if (score < 70) {
color = getResources().getColor(R.color.bg_orangef);
} else {
color = getResources().getColor(R.color.bg_green11);
}
ClipDrawable d = new ClipDrawable(new ColorDrawable(color), Gravity.
LEFT, ClipDrawable.HORIZONTAL);
progressBar.setProgressDrawable(d);
progressBar.setProgress(score);
}
这里要说明一下ClipDrawable这个类:ClipDrawable 是对一个Drawable进行剪切操作,可以控制这个drawable的剪切区域,以及相相对于容器的对齐方式,Android中的进度条就是使用一个ClipDrawable实现效果的,它根据level的属性值,决定剪切区域的大小。也就是说该类值把有进度值得地方截取下来,其余的为空白,所以为了看的清楚我将progressbar嵌套在linearlayout中背景设置为灰色:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#848484">
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:max="100" />
</LinearLayout>
调用:
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_and_security);
String password = passwordStrong(SharedPreferenceManager.getUserPassword());
init();
String temp = getResources().getString(R.string.str_password_grade);
String passwordGrade = String.format(temp, password);
tv_aqdj.setText(passwordGrade);
setProgressBarColour(GRADE_SCORE);
}
效果展示: