1. 监听软键盘

由于官方没有提供相关的监听,只能通过界面布局来判断软键盘显示和隐藏。

  • 通过OnLayoutChangeListener来监听

      getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {          @Override          public void onLayoutChange(View v, int left, int top, int right, int bottom,                  int oldLeft, int oldTop, int oldRight, int oldBottom) {          }      });
  • 通过OnGlobalLayoutListener来监听

      getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(      new ViewTreeObserver.OnGlobalLayoutListener() {          @Override          public void onGlobalLayout() {          }      });
  • 判断软键盘显示和隐藏。

      private boolean isShowing() {      // 获取当前屏幕内容的高度      int screenHeight = getWindow().getDecorView().getHeight();      // 获取View可见区域的bottom      Rect rect = new Rect();      getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);      return screenHeight > rect.bottom;  }

    不过在某些导航栏存在的手机上会发生某些问题,可以通过获取导航栏高度来避免。

      private int getNavigatorBarHeight() {      int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");      int height = getResources().getDimensionPixelSize(resourceId);      LogTool.logi("SoftManagerListenerActivity", "Status Bar Height = " + height);      return height;  }

2. 软键盘显示

软键盘显示时可能会覆盖某些控件,必要时需要移动界面。

private void onLayout() {    //获取当前屏幕内容的高度    int screenHeight = getWindow().getDecorView().getHeight();    //获取View可见区域的bottom    Rect rect = new Rect();    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);    if (screenHeight - getNavigatorBarHeight() > rect.bottom) {        // mTranslate用来记录是否已经移动        if (!mTranslate) {            // 获取按钮的左上角,按钮高度为40dp            int[] location = new int[2];            mBtnNext.getLocationOnScreen(location);            int bottom = location[1] + getResources().getDimensionPixelSize(R.dimen.margin_dpi_50);            // 如果按钮被覆盖,移动整个界面向上移动            if (bottom > rect.bottom) {                getWindow().getDecorView().scrollBy(0, bottom - rect.bottom);                mTranslate = true;            }        }    } else {        getWindow().getDecorView().scrollTo(0, 0);        mTranslate = false;    }}

效果如下

参考资料:https://www.cnblogs.com/shelly-li/p/5639833.html
参考资料:http://blog.csdn.net/sinat_31311947/article/details/53914000

相关文章
Android TextView控件
Android Span应用
Android EditText控件
Android 监听软键盘显示和隐藏

更多相关文章

  1. android 界面应用锦集
  2. 【LatinIME】默认开启键盘按键声
  3. Android:时间控件
  4. Android各种花式酷炫自定义控件开源库集合(1)。
  5. Android控件开发之Gallery
  6. android View的三个构造方法 简单总结
  7. Android(安卓)Widget开发
  8. Android处理EditText键盘自动隐藏
  9. Android之动态改变控件大小

随机推荐

  1. Android(安卓)拍照 以及从本地选择图片
  2. 管理图片缓存
  3. 使用calabash测试开源中国Android客户端
  4. 【Android 开发教程】ListView的基本使用
  5. Android底层驱动开发 -驱动配置篇
  6. Android(安卓)Activity切换与Activity间
  7. Android(安卓)通过 地名 获得 经纬度 并
  8. Android 带小圆圈的倒计时圆形进度条
  9. Android 如何更换屏幕上锁界面背景图片
  10. 从菜鸟到Android资深工程师的进阶之路