Android PopupWindow 隐藏软键盘的方法



今天,简单讲讲android里


PopupWindow 弹出在底部,被软键盘遮挡的问题。


之前,自己写一个PopupWindow 弹出在底部,可是如果软键盘显示时弹出,软键盘会遮挡PopupWindow ,在网上搜索了资料,终于解决了这个问题。这里记录一下。


1.解决软键盘的弹出会挡住PopupWindow的问题。

//设置弹出窗体需要软键盘
commentWindow.setSoftInputMode(AddCommentPopupWindow.INPUT_METHOD_NEEDED);

//设置模式,和Activity的一样,覆盖,调整大小。
commentWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


这里对软键盘弹出方式进行了设置,在网上查的有的说用下面一句,有的说两句都要有。我用的是两句话。更多详情请看:Android windowSoftInputMode属性详解(多谢博主分享)


这里简单讲讲一下,PopupWindow.INPUT_METHOD_NEEDED是设置PopupWindow 弹出时隐藏软件盘,WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE这个属性是设置软键盘弹出时,PopupWindow 主窗口会被调整大小,从而保证软键盘显示空间。这个可以解决activity里弹出软键盘和PopupWindow 里的Edittext弹出软键盘遮挡PopupWindow 的问题。


如果只是activity的软键盘遮挡PopupWindow ,那么在弹出PopupWindow 时可以手动设置隐藏软键盘。也很简单:

2.软键盘弹出的方法

InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//这里给它设置了弹出的时间,
imm.toggleSoftInput(1000, InputMethodManager.HIDE_NOT_ALWAYS);


这个我在网上查了很多,也尝试了,只有这个方法成功了。不知道为什么,求大神解释一下,谢谢。

3.软键盘消失的方法

//参数:1,自己的EditText。2,时间。
imm.hideSoftInputFromWindow(edt_AddComment_Pop_LRDA.getWindowToken(), 0);

这个方法的第一个参数需要传入自己的EditText对象。比如QQ空间的评论,当点击发送的时候调用此方法即可。


关于软键盘的弹出与隐藏,我会专门再写一篇博客,大家有兴趣也可以自己去查找资料。


Android PopupWindow 隐藏软键盘的方法就讲完了。


就这么简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow弹出时,可以通过设置PopupWindow的softInputMode属性来控制软键盘显示隐藏。一般情况下,我们可以将PopupWindow的softInputMode属性设置为PopupWindow.INPUT_METHOD_NEEDED,表示PopupWindow需要软键盘,但是PopupWindow并不会自动调整大小以避免遮盖软键盘。 因此,为了解决PopupWindow遮住软键盘的问题,我们可以在PopupWindow弹出时,手动调整PopupWindow的大小,使其避开软键盘。具体实现方法如下: 1. 获取当前软键盘的高度: ```java public int getSoftInputHeight(Activity activity) { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight(); int softInputHeight = screenHeight - rect.bottom; if (softInputHeight > 0 && softInputHeight < screenHeight / 3) { return softInputHeight; } return 0; } ``` 2. 在PopupWindow弹出时,获取软键盘高度并调整PopupWindow的大小: ```java mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); // 监听PopupWindow的布局变化 mPopupWindow.getContentView().getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int softInputHeight = getSoftInputHeight(activity); if (softInputHeight > 0) { // 获取PopupWindow的布局参数 ViewGroup.LayoutParams layoutParams = mPopupWindow.getContentView().getLayoutParams(); layoutParams.height = screenHeight - softInputHeight; mPopupWindow.getContentView().setLayoutParams(layoutParams); } } }); ``` 这样,当软键盘弹出时,PopupWindow就会自动调整大小以避开软键盘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值