class:
private CharSequence temp;//监听前的文本private Button bt_affirm;//按钮 bt_affirm = (Button) findViewById(R.id.bt_affirm);
private EditText et_pwd;//密码 et_pwd = (EditText) findViewById(R.id.et_pwd);
et_pwd.addTextChangedListener( new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
temp = s;
}
@Override
public void afterTextChanged(Editable s) {
if (temp.length() >5) {//长度大于5,改变按钮背景
bt_affirm.setBackgroundResource(R.drawable.bt_blue);
}else {
bt_affirm.setBackgroundColor(color.color_gray);
}
}
});
layout:
<EditText
android:id="@+id/et_pwd"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:background="@null"
android:maxLength="16"
android:singleLine="true"
android:password="true"
android:hint="请输入登录密码"
android:textSize="15sp" />
<Button
android:id="@+id/bt_affirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="@color/color_gray"
android:text="确认"
android:textColor="@color/color_white"
android:textSize="20sp" />