总结TextWatcher两种使用场景

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("数据改变,执行更新保存");
                }
            }
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值