zoukankan      html  css  js  c++  java
  • android开发获取键盘高度以及判断键盘是否显示(兼容分屏模式)

    android开发获取键盘高度以及判断键盘是否显示

    //方法一(兼容分屏模式):反射获取键盘高度,,,-1表示反射失败,0表示键盘隐藏,大于0是键盘高度表示键盘显示。。。
    //关于android 9 之后非公开api调用黑名单表格hiddenapi-flags.csv链接:https://developer.android.google.cn/guide/app-compatibility/restrictions-non-sdk-interfaces
    public int getKeyboardHeight(Context context){
       try {
          InputMethodManager im = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
          Method method = im.getClass().getDeclaredMethod("getInputMethodWindowVisibleHeight");
          method.setAccessible(true);
          Object height = method.invoke(im);
          return Integer.parseInt(height.toString());
       }catch (Throwable e){
          return -1;
       }
    }
    
    //方法二(分屏模式可能失效):添加ViewTreeObserver判断decorView的高度和contentView的高度,可以大致判断,不过这种方式在分屏模式可能失效
    View contentView = findViewById(R.id.content_view);
    contentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //contentView.getRootView()即得到的是decorView
            int heightDiff = contentView.getRootView().getHeight() - contentView.getHeight();
            if (heightDiff > 0.25 * contentView.getRootView().getHeight()) { 
               // if more than 25% of the screen, its probably a keyboard is showing...
               // do something here
            }
        }
    }); 
        
    
  • 相关阅读:
    PL/SQL Developer 远程连接Oracle数据库
    常用js代码学习
    三元运算 多条件嵌套
    在后台CS文件里面,隐藏和显示Repeater里面控件
    最小二乘线性及平面拟合原理及C++实现
    C++的顶层const 和 底层const
    vs2019上遇到的一个未预指定引用函数的静态库导致的问题
    一个基础QItemDelegate检查器类
    linux 基础命令及快捷键
    lambda表达式
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/15077123.html
Copyright © 2011-2022 走看看