在我的应用程序中,我有一个EditText
和一个Switch
(查看),但我想要关闭EditText
的Switch不可编辑。此外,当Switch处于启用状态时,EditText
应该是可编辑的。
我的Switch听众:
infoSerialFrag_rememberSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
isRemember = true;
} else {
isRemember = false;
}
}
});
我该怎么做?
答案 0 :(得分:0)
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[] {});
}
}
});
答案 1 :(得分:0)
试试这段代码:
infoSerialFrag_rememberSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
isRemember = true;
} else {
isRemember = false;
}
if (isRemember) {
infoSerialFrag_rememberSwitchText.setFocusableInTouchMode(true);
} else {
infoSerialFrag_rememberSwitchText.setFocusable(false);
}
}
});
} else {
hideView(infoSerialFrag_rememberSwitch);
}
if (isRemember) {
infoSerialFrag_rememberSwitchText.setFocusableInTouchMode(true);
} else {
infoSerialFrag_rememberSwitchText.setFocusable(false);
}