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

Kotlin:Inflate加载的PopupWindow内EditText软键盘显示在弹窗后方

全屏PopupWindow软键盘被遮挡问题修复

问题复现

通过inflate加载全屏PopupWindow时,软键盘显示在弹窗后方,被黑色不透明蒙层遮挡,对应实现代码:

val inflater = it.context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
val menuPopup = inflater!!.inflate(R.layout.popup_search_new_cosmetics,null)

val popup = PopupWindow(menuPopup, WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.MATCH_PARENT, false)
popup.showAtLocation(menuPopup, Gravity.CENTER,0,0)
val imgClose = menuPopup.findViewById<ImageView>(R.id.imageClose)
menuPopup.findViewById<ImageView>(R.id.imageClose)
recycler = menuPopup.findViewById(R.id.editProductPage)

showKeyboard(menuPopup.findViewById(R.id.editTextSearch),this)


private fun showKeyboard(mEtSearch: EditText, context: Context) {
    mEtSearch.requestFocus()
    val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

异常效果:
软键盘被蒙层遮挡效果

修复步骤

  • 配置PopupWindow的软键盘适配模式,在调用showAtLocation前添加代码:
    popup.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
  • 为PopupWindow设置透明背景,避免默认黑色背景干扰窗口层级,和上述配置放在同一位置:
    popup.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
  • 延迟触发软键盘弹出,等PopupWindow视图完成挂载后再执行弹键盘逻辑,替换原有直接调用showKeyboard的代码:
menuPopup.post {
    showKeyboard(menuPopup.findViewById(R.id.editTextSearch), this@CurrentPageActivity)
}
  • 修正软键盘弹出方法,替换toggleSoftInput为绑定输入框的showSoftInput调用,避免软键盘窗口层级错误:
private fun showKeyboard(mEtSearch: EditText, context: Context) {
    mEtSearch.requestFocus()
    val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(mEtSearch, InputMethodManager.SHOW_IMPLICIT)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 01:54:29