如何在Android中使用Java设置可编辑的EditText?

时间:2017-08-02 17:39:35

标签: java android android-edittext

在我的应用程序中,我有一个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;
        }
    }
});

我该怎么做?

2 个答案:

答案 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);
}