在我的应用程序中,我有一个EditText
和Switch
(视图),但是我想如果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[] {});
}
}
});