更改然后重置属性后,EditText 不再可聚焦

EditText stops being focusable after changing and then reseting attributes

在我的项目中,我将 Edittexts 伪装成文本视图,用户只能在启用编辑模式后进行编辑。我为此设置了2个扩展函数

private fun EditText.disguiseAsTextview() {
    this.isCursorVisible = false
    this.isLongClickable = false
    this.isClickable = false
    this.isFocusable = false
    this.isSelected = false
    this.setBackgroundResource(android.R.color.transparent)
}

private fun EditText.breakDisguise() {
    this.isCursorVisible = true
    this.isLongClickable = true
    this.isClickable = true
    this.isFocusable = true
    this.background = editTextDrawableBackground
}

一开始EditText可以正常工作,但是经过伪装和脱伪后,EditText无法再聚焦,并且闪烁蓝色,就像被选中一样。不知道为什么会这样,因为在调用 breakDisguise 后将所有内容都设置回默认状态。

在你的breakDisguise方法上,

替换

this.isFocusable = true

this.isFocusableInTouchMode = true