本文主要是介绍android PopWindow弹不出输入法和遮挡输入框的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景,在popWindow弹输入法,注意两个地方,
- 当前版本号
- pop要获取焦点
兼容版本号
弹出软键盘
private fun showSoftInput() {if (Build.VERSION.SDK_INT >= 30) {"版本大于等 30".log()binding.etNewUser.windowInsetsController?.show(WindowInsetsCompat.Type.ime())} else {// 获取焦点拉起软键盘binding.etNewUser.requestFocus()val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManagerimm.showSoftInput(binding.etNewUser, InputMethodManager.SHOW_IMPLICIT)}}
隐藏软键盘
private fun hideSoftInput() {if (Build.VERSION.SDK_INT >= 30) {"版本大于等 30".log()binding.etNewUser.windowInsetsController?.hide(WindowInsetsCompat.Type.ime())} else {val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManagerimm.hideSoftInputFromWindow(binding.root.windowToken, 0)}}
获取焦点
fun show(parent: View) {isFocusable = trueshowAtLocation(parent, Gravity.NO_GRAVITY,0,0)}
遮挡输入框
思路:用父布局的原高度 + 软键盘弹出的高度 设置为父布局的新高度,软键盘就实际是覆盖了空白的部分,就不会遮挡住原popupWindow的内容了
注意:不要在根布局使用ConstraintLayout,使用LinearLayout或者其他布局。
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;/*** Created by KillaXiao on 2019/8/14.* 监听软键盘的弹出与收起, 在部分手机测试到,popupwindow里面有edittext的时候,弹出软键盘,会覆盖掉从底部弹出的popupwindow内容*/
public class SoftKeyBoardListener {private View rootView; //activity的根视图int rootViewVisibleHeight; //纪录根视图的显示高度private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;public SoftKeyBoardListener(Activity activity) {//获取activity的根视图rootView = activity.getWindow().getDecorView();Rect r = new Rect();rootView.getWindowVisibleDisplayFrame(r);int visibleHeight = r.height();rootViewVisibleHeight = visibleHeight;//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {//获取当前根视图在屏幕上显示的大小Rect r = new Rect();rootView.getWindowVisibleDisplayFrame(r);int visibleHeight = r.height();System.out.println(""+visibleHeight);if (rootViewVisibleHeight == 0) {rootViewVisibleHeight = visibleHeight;return;}//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变if (rootViewVisibleHeight == visibleHeight) {return;}//根视图显示高度变小超过200,可以看作软键盘显示了if (rootViewVisibleHeight - visibleHeight > 200) {if (onSoftKeyBoardChangeListener != null) {onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);}rootViewVisibleHeight = visibleHeight;return;}//根视图显示高度变大超过200,可以看作软键盘隐藏了if (visibleHeight - rootViewVisibleHeight > 200) {if (onSoftKeyBoardChangeListener != null) {onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);}rootViewVisibleHeight = visibleHeight;return;}}});}private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;}public interface OnSoftKeyBoardChangeListener {void keyBoardShow(int height);void keyBoardHide(int height);}public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);}
}
在popwindow中使用:
private fun softInputListener(context:Activity){SoftKeyBoardListener.setListener(context,object:SoftKeyBoardListener.OnSoftKeyBoardChangeListener{override fun keyBoardShow(height: Int) {"keyBoardShow : $height".log()val layoutParams = binding.root.layoutParams//用父布局的原高度 + 软键盘弹出的高度 设置为父布局的新高度,软键盘就实际是覆盖了空白的部分,就不会遮挡住原popupWindow的内容了layoutParams.height = binding.root.measuredHeight+heightbinding.root.layoutParams = layoutParamsbinding.root.invalidate()}override fun keyBoardHide(height: Int) {"keyBoardHide : $height".log()//关闭软键盘时,再把布局高度设置回来val layoutParams = binding.root.layoutParamslayoutParams.height = binding.root.measuredHeight-heightbinding.root.layoutParams = layoutParamsbinding.root.invalidate()}})}
这篇关于android PopWindow弹不出输入法和遮挡输入框的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!