1. 观察多个输入框(必填)是否输入内容,控制按钮是否可用。
public class ButtonWatcher {
private final Button btn; //控制按钮
private final EditText[] views; //输入框数组
public ButtonWatcher(Button btn, EditText... editTexts) {
this.btn = btn;
this.views = editTexts;
this.btn.setEnabled(false); //默认不可用
for (EditText v : editTexts) {
v.addTextChangedListener(textWatcher);
}
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean flag = false;
for (EditText v : views) {
if (TextUtils.isEmpty(v.getText().toString())) {
flag = true;
break;
}
}
btn.setEnabled(!flag);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
};
}
xml布局
<?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:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_btn_enabled"
android:text="保存" />
</LinearLayout>
按钮背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 不可用背景 -->
<item android:drawable="@color/btn_enabled_color" android:state_enabled="false" />
<!-- 默认背景 -->
<item android:drawable="@color/btn_color" />
</selector>
代码调用
EditText nameEditText = (EditText) findViewById(R.id.name);
EditText passwordEditText = (EditText) findViewById(R.id.password);
Button saveButton = (Button) findViewById(save);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
L.d("数据已全部填写,按钮可用, 执行保存");
}
});
new ButtonWatcher(saveButton, nameEditText, passwordEditText);
2. 观察多个输入框内容是否改变。
public class ChangeWatcher {
private boolean change; //内容是否改变
private final EditText[] views; //输入框数组
private List<String> originalTexts; //输入框原始数据
public ChangeWatcher(EditText... editTexts) {
this(true, editTexts);
}
public ChangeWatcher(boolean enalbe, EditText... editTexts) {
this.views = editTexts;
this.originalTexts = new ArrayList<>();
for (EditText v : editTexts) {
v.setEnabled(enalbe);
v.addTextChangedListener(textWatcher);
originalTexts.add(v.getText().toString());
}
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
for (int i = 0; i < views.length; i++) {
if (!originalTexts.get(i).equals(views[i].getText().toString())) {
change = true;
break;
} else {
change = false;
}
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
};
public boolean isChange() {
return change;
}
public void setEditViewEnabled(boolean enable) {
for (EditText v : views) {
v.setEnabled(enable);
}
}
代码调用
Button saveButton = (Button) findViewById(save);
EditText nameEditText = (EditText) findViewById(R.id.name);
EditText passwordEditText = (EditText) findViewById(R.id.password);
nameEditText.setText("名字");
passwordEditText.setText("12345678");
final ChangeWatcher changeWatcher = new ChangeWatcher(nameEditText, passwordEditText);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (changeWatcher.isChange()){
L.d("数据改变,执行更新保存");
}
}
});