先上效果图
psd_edt.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { String str = psd_edt.getText().toString().trim(); int length = str.length(); if (length > 0) { if (!AbStrUtil.isNumberLetter(str)) { str = str.substring(0, length - 1); psd_edt.setText(str); String str1 = psd_edt.getText().toString().trim(); psd_edt.setSelection(str1.length()); psd_edt.setError("密码只能是字母和数字"); } } //输入框为0 if (str.length() == 0) { pwd_weak.setBackgroundColor(Color.rgb(205,205,205)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的纯数字为弱 if (str.matches ("^[0-9]+$")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的纯小写字母为弱 else if (str.matches ("^[a-z]+$")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的纯大写字母为弱 else if (str.matches ("^[A-Z]+$")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和数字,输入的字符小于7个密码为弱 else if (str.matches ("^[A-Z0-9]{1,5}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和数字,输入的字符大于7个密码为中 else if (str.matches ("^[A-Z0-9]{6,16}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(255,184,77)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的小写字母和数字,输入的字符小于7个密码为弱 else if (str.matches ("^[a-z0-9]{1,5}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的小写字母和数字,输入的字符大于7个密码为中 else if (str.matches ("^[a-z0-9]{6,16}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(255,184,77)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和小写字母,输入的字符小于7个密码为弱 else if (str.matches ("^[A-Za-z]{1,5}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和小写字母,输入的字符大于7个密码为中 else if (str.matches ("^[A-Za-z]{6,16}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(255,184,77)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和小写字母和数字,输入的字符小于5个个密码为弱 else if (str.matches ("^[A-Za-z0-9]{1,5}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(205,205,205)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和小写字母和数字,输入的字符大于6个个密码为中 else if (str.matches ("^[A-Za-z0-9]{6,8}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(255,184,77)); pwd_strong.setBackgroundColor(Color.rgb(205,205,205)); } //输入的大写字母和小写字母和数字,输入的字符大于8个密码为强 else if (str.matches ("^[A-Za-z0-9]{9,16}")) { pwd_weak.setBackgroundColor(Color.rgb(255,129,128)); pwd_in.setBackgroundColor(Color.rgb(255,184,77)); pwd_strong.setBackgroundColor(Color.rgb(113,198,14)); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } });
源码传送:http://download.csdn.net/detail/wj198524/9778821