最近在项目中,需要获取到软键盘的高度,再将底部的布局上移键盘的高度,话不多说,直接上代码:

获取软键盘高度

    //一个静态变量存储高度public static int keyboardHeight = 0;boolean isVisiableForLast = false;ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = null;public void addOnSoftKeyBoardVisibleListener() {if (keyboardHeight > 0) {return;}final View decorView = getWindow().getDecorView();onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {Rect rect = new Rect();decorView.getWindowVisibleDisplayFrame(rect);//计算出可见屏幕的高度int displayHight = rect.bottom - rect.top;//获得屏幕整体的高度int hight = decorView.getHeight();boolean visible = (double) displayHight / hight < 0.8;int statusBarHeight = 0;try {Class<?> c = Class.forName("com.android.internal.R$dimen");Object obj = c.newInstance();Field field = c.getField("status_bar_height");int x = Integer.parseInt(field.get(obj).toString());statusBarHeight = getApplicationContext().getResources().getDimensionPixelSize(x);} catch (Exception e) {e.printStackTrace();}if (visible && visible != isVisiableForLast) {//获得键盘高度keyboardHeight = hight - displayHight - statusBarHeight;Log.i("keyboardHeight==1213==", "" + keyboardHeight);}isVisiableForLast = visible;}};decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);}

键盘的打开与关闭操作:

public class KeybordS {/*** 打开软键盘*/public static void openKeybord(EditText mEditText, Context mContext) {mEditText.setFocusable(true);mEditText.setFocusableInTouchMode(true);mEditText.requestFocus();InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);}/*** 关闭软键盘*/public static void closeKeybord(EditText mEditText, Context mContext) {InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);}/*** 关闭软键盘*/public static void hideInput(Activity activity) {if (activity.getCurrentFocus() != null) {InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);}}/*** 判断当前软键盘是否打开*/public static boolean isSoftInputShow(Activity activity) {// 虚拟键盘隐藏 判断view是否为空View view = activity.getWindow().peekDecorView();if (view != null) {// 隐藏虚拟键盘InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//       inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;}return false;}}

监听键盘处于打开还是关闭状态:

private void setListenerToRootView() {final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {boolean mKeyboardUp = isKeyboardShown(rootView);if (mKeyboardUp) {//键盘弹出//Toast.makeText(getApplicationContext(), "键盘弹出", Toast.LENGTH_SHORT).show(); } else {//键盘收起//Toast.makeText(getApplicationContext(), "键盘收起", Toast.LENGTH_SHORT).show();}}});
}private boolean isKeyboardShown(View rootView) {final int softKeyboardHeight = 100;Rect r = new Rect();rootView.getWindowVisibleDisplayFrame(r);DisplayMetrics dm = rootView.getResources().getDisplayMetrics();int heightDiff = rootView.getBottom() - r.bottom;return heightDiff > softKeyboardHeight * dm.density;
}

小编整理了一份Android电子书籍,需要的童鞋关注底部公众号(longxuanzhigu)回复:“e_books” 即可获取哦!

以下是个人公众号(longxuanzhigu),之后发布的文章会同步到该公众号,方便交流学习Android知识及分享个人爱好文章:

Android获取软键盘的高度、键盘的打开与关闭、监听键盘处于打开还是关闭状态相关推荐

  1. android 获取ListView内部Item高度

    今天,简单讲讲android 如何  获取ListView内部Item高度. 这个比较简单,但是之前自己不知道怎么做,在网上查了资料才找到.所以记录一下. 在外面取: ListAdapter li ...

  2. android获得键盘高度,Android获取软键盘高度

    在 Android 里我们是无法直接获取软键盘高度的,但是在某些场景下,我们又需要获取软键盘的高度.我们可以使用 ViewTreeObserver.OnGlobalLayoutListener来监听窗 ...

  3. Android 获取软键盘的删除delete事件

    对于软键盘删除事件,网上有很多方案是如下,但是 google api也说明了,这个只是监听硬件键盘,对于软键盘并不负责触发(我测试了一下,软键盘能够监听delete键,其他键像数字字母等没有触发这里的 ...

  4. Android 获取底部导航条高度

    获取设备底部导航条高度 /*** 获取底部导航条高度*/ private fun getNavigationBarHeight(): Int {if (!isNavigationBarShow()) ...

  5. android 虚拟键 高度,Android获取虚拟按键的高度(适配全面屏)

    在开发过程中,假如我们要从底部弹出一个popwindow,这时候我们需要考虑获取实际显示的虚拟键高度. 期望: 全面屏下 1.1 开启全面屏开关-返回0 1.2 关闭全面屏开关-执行非全面屏下处理方式 ...

  6. Android获取软键盘输入内容

    该功能的实现是通过Android辅助功能来进行实现的, 先上效果图:                                                      下面说一下如何使用Acc ...

  7. android获取导航栏宽高,Android获取屏幕的宽高度、状态栏、标题栏、导航栏、编辑区域高度...

    目录 0.相关文章: 1.获取屏幕款高度 代码: /** * 获取屏幕宽度 * * @param context 上下文对象 * @return int */ public static int ge ...

  8. Android获取顶部状态栏statusBar高度、底部导航栏navigationBar高度

    手机顶部显示时间.电量等信息的叫状态栏,即statusBar 有些手机比如华为在底部会有返回.回到主页面等虚拟功能键,这是导航栏,即navigationBar 下面列出一些项目中常用的功能代码? 嘻嘻 ...

  9. android获取刘海屏状态栏高度,Android刘海屏全面屏底部导航栏的适配

    关于Android状态栏和虚拟导航栏的适配,文章:https://blog.csdn.net/leogentleman/article/details/54566319 讲的很不错. 状态栏的适配: ...

最新文章

  1. 如何将django部署从顶级目录迁移到子目录下(NGINX UWSGI DJANGO)
  2. pwntools用docker实现,进行pwn题解答
  3. boost::shared_ptr相关的测试程序
  4. 元件原理图旋转45度_大口径大曲率半径光学元件的高精度检测
  5. XV6操作系统代码阅读心得(二):进程
  6. LuckyDraw app使用CosmosDB的成本分析
  7. 排序数组中的两个数字之和
  8. FFMPEG详解(完整版)
  9. 解决 “error while loading shared libraries: libpng12.so.0: cannotopen shared object file: No such fil
  10. 新萝卜家园 GhostXP SP3 电脑城装机版 V2011.07
  11. 10分钟明白为什么要使用微服务
  12. iOS 9适配技巧(更新版)
  13. Java并发指南6:Java内存模型JMM总结
  14. 交叉验证中cv=? 与 cv=KFold(n_splits=?)的区别
  15. java对象转xml 高性能_xml与java对象的快速互转
  16. java制作SM2证书
  17. 学习黑客技术打败黑客才是突破技术巅峰!
  18. 苹果切换输入法_轻松搞定缅甸语手机输入法(苹果篇)
  19. 《Spring中的自动装配》
  20. 英语不好可以学编程嘛?程序员必备英文单词汇总

热门文章

  1. ECSHOP_模版文件全介绍
  2. Spring Security OAuth2.0认证授权三:使用JWT令牌
  3. 无盘主机服务器,无盘主机服务器视频
  4. 安装pycharm专业版
  5. BorderDet:Border Feature for Dense ObjectDetection
  6. 2021-08-05 JavaScript
  7. R包WGCNA---转录组WGCNA共表达网络构建(无表型计算提取网络)
  8. 覆盖率报告同步到Sonar中遇到的问题 小结
  9. 冠位英灵官方设定_fate:虽是低星从者,设定却超级强大,都拥有冠位英灵的实力...
  10. java从网站上获取图片