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
相关产品推荐
相关产品推荐

