首先写一个布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/v_pas_1"
style="@style/view_line_pas"
android:layout_width="0dp"
android:layout_weight="1" />
<View style="@style/view_line_shu" />
<ImageView
android:id="@+id/v_pas_2"
style="@style/view_line_pas"
android:layout_width="0dp"
android:layout_weight="1" />
<View style="@style/view_line_shu" />
<ImageView
android:id="@+id/v_pas_3"
style="@style/view_line_pas"
android:layout_width="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/tx_pas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weak"
android:paddingLeft="1dp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/text_size_small" />
</LinearLayout>
自定义一个布局(其中
)<color name="pass_yellow">#FC9900</color> <color name="pass_red">#c5101a</color> <color name="pass_green">#2BF927</color> <color name="pass_gray">#B2B2B2</color>
public class EditPassLinearLayout extends LinearLayout {
private final static String TAG = "MyPassLinearLayout";
private EditText etListen;
private ImageView view1, view2, view3;
private String password;
private TextView txTips;
private int PassWordStatus = 0;
public MyPassLinearLayout(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.linerlayout_pass, this);
initUI();
}
public MyPassLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.linerlayout_pass, this);
initUI();
}
public void setEditextListener(EditText etListen) {
this.etListen = etListen;
initData();
}
private void initUI() {
view1 = (ImageView) findViewById(R.id.v_pas_1);
view2 = (ImageView) findViewById(R.id.v_pas_2);
view3 = (ImageView) findViewById(R.id.v_pas_3);
txTips = (TextView) findViewById(R.id.tx_pas);
}
private void initData() {
etListen.addTextChangedListener(textWatcher);
//EditText获得焦点的监听
etListen.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i(TAG, "hasFocus--->" + hasFocus);
if (hasFocus) {
//如果获得焦点就显示,否则隐藏
show();
} else {
dismiss();
}
}
});
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
password = etListen.getText().toString().trim();
showLLPassWord(password);
}
};
//展示密码的强度
void showLLPassWord(String pass) {
switch (getPasswordStatus) {
case 0:
txTips.setText(R.string.weak);
view1.setBackgroundColor(Utils.getColorByResouse(R.color.pass_gray));
view2.setBackgroundColor(Utils.getColorByResouse(R.color.pass_gray));
view3.setBackgroundColor(Utils.getColorByResouse(R.color.pass_gray));
break;
case 1:
txTips.setText(R.string.weak);
view1.setBackgroundColor(Utils.getColorByResouse(R.color.pass_red));
view2.setBackgroundColor(Utils.getColorByResouse(R.color.pass_gray));
view3.setBackgroundColor(Utils.getColorByResouse(R.color.pass_gray));
break;
case 2:
txTips.setText(R.string.center);
view1.setBackgroundColor(Utils
.getColorByResouse(R.color.pass_yellow));
view2.setBackgroundColor(Utils
.getColorByResouse(R.color.pass_yellow));
view3.setBackgroundColor(Utils.getColorByResouse(R.color.pass_gray));
break;
case 3:
txTips.setText(R.string.strong);
view1.setBackgroundColor(Utils.getColorByResouse(R.color.green));
view2.setBackgroundColor(Utils.getColorByResouse(R.color.green));
view3.setBackgroundColor(Utils.getColorByResouse(R.color.green));
break;
default:
break;
}
}
public void show() {
this.setVisibility(View.VISIBLE);
}
public void dismiss() {
this.setVisibility(View.GONE);
}
/**
* 获得密码等级
*
* @return 密码等级
*/
public int getPasswordStatus() {
定义密码强度的计算方法
return PassWordStatus;
}
/**
* 是否弱密码
* @return
*/
public boolean isWeakpassword() {
if (PassWordStatus < 2) {
return true;
}
return false;
}
}
使用
<pre name="code" class="html"> <EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:hint="pass"
android:singleLine="true"
android:text=""
android:textColor="@color/text_color_black"
android:textSize="@dimen/text_size_edit" />
<com.demo.EditPassLinearLayout
android:id="@+id/pass"
<pre name="code" class="java"> android:layout_width="match_parent"
android:layout_height="<span style="color:#008000;">20dp</span>"
/>
editPassLinearLayout.setEditextListener(editText);
这样就ok了