在我的应用程序中,我有一个EditTextSwitch(视图),但是我想如果EditText的开关关闭,则不可编辑。同样,当开关打开时,EditText应该是可编辑的。

我的Switch侦听器:

infoSerialFrag_rememberSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if (isChecked) {
            isRemember = true;
        } else {
            isRemember = false;
        }
    }
});


我怎样才能做到这一点?

最佳答案

infoSerialFrag_rememberSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if (isChecked) {
            isRemember = true;
            editText.setFilters(new InputFilter[] {
                new InputFilter() {
                    public CharSequence filter(CharSequence src, int start, int end, Spanned dst, int dstart, int dend) {
                        return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
                    }
                }
            });
        } else {
            isRemember = false;
            editText.setFilters(new InputFilter[] {});
        }
    }
});

07-26 01:54