You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改并重置属性后EditText失去可聚焦性问题求助

问题根因

你遗漏了isFocusableInTouchMode属性的配置:Android触摸交互模式下,控件要稳定获取输入焦点需要同时满足isFocusable = true和isFocusableInTouchMode = true两个条件。当你在disguiseAsTextview方法中将isFocusable设为false时,系统会自动同步将isFocusableInTouchMode也置为false,但你在breakDisguise恢复属性时仅修改了isFocusable,没有恢复触摸模式下的可聚焦状态,就会出现点击后仅短暂选中、无法持续持有焦点的问题。

修复后的代码

private fun EditText.disguiseAsTextview() {
    this.isCursorVisible = false
    this.isLongClickable = false
    this.isClickable = false
    this.isFocusable = false
    // 同步关闭触摸模式可聚焦属性
    this.isFocusableInTouchMode = 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.isFocusableInTouchMode = true
    this.background = editTextDrawableBackground
    // 可选:恢复后主动请求焦点,自动弹出输入法
    this.requestFocus()
}

如果恢复后输入法没有自动弹出,可以额外添加调用逻辑:

val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 00:15:00