android 隐藏输入法

编程入门 行业动态 更新时间:2024-10-25 00:26:36

android 隐藏<a href=https://www.elefans.com/category/jswz/34/1767193.html style=输入法"/>

android 隐藏输入法

试过hideSoftInputFromWindow不起作用,只有toggleSoftInput其作用,但是这个方法是:输入法隐藏的时候显示,输入法显示的时候隐藏。我的应用场景是dialogFragment关闭的时候要把输入法隐藏,如果输入法本身就是隐藏状态,那么输入法会显示出来,这样是不对的。所以要加一个判断输入法是否是显示状态(参考)

private boolean isInputMethodShowing() {//获取当前屏幕内容的高度int screenHeight = getActivity().getWindow().getDecorView().getHeight();//获取View可见区域的bottomRect rect = new Rect();getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);return screenHeight - rect.bottom - getSoftButtonsBarHeight() != 0;}/*** 底部虚拟按键栏的高度* @return*/@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)private int getSoftButtonsBarHeight() {DisplayMetrics metrics = new DisplayMetrics();//这个方法获取可能不是真实屏幕的高度getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);int usableHeight = metrics.heightPixels;//获取当前屏幕的真实高度getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);int realHeight = metrics.heightPixels;if (realHeight > usableHeight) {return realHeight - usableHeight;}else {return 0;}}

然后隐藏输入法

    //如果输入法在窗口上已经显示,则隐藏,反之则显示private void hideSoftInputWindow(Context context) {InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);if(isInputMethodShowing()) {LogUtils.i("hideSoftInputWindow:");imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);}}

 

更多推荐

android 隐藏输入法

本文发布于:2023-06-22 05:11:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:输入法   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!