码迷,mamicode.com
首页 > 移动开发 > 详细

android监听软键盘事件并获取键盘高度

时间:2018-02-07 21:20:34      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:turn   col   pre   软键盘   ldl   监听   sum   void   事件   

@Override
  public void onResume(){
    super.onResume();
    //获取当前屏幕内容的高度
    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) {
        //获取View可见区域的bottom
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

        //避免重复事件
        String keyboardStatus = String.format("%d%d%d",bottom,oldBottom,rect.bottom);
        if(keyboardStatus.equals(lastKeyboardStatus)){
          return;
        }
        lastKeyboardStatus = keyboardStatus;
        if(bottom!=0 && oldBottom!=0 && bottom - rect.bottom <= 0){
          HashMap<String,Object> data = new HashMap<String, Object>();
          data.put("eventName","hide");
          ImUtils.dispatch("keyBoardEvent",data);
        }else {
          HashMap<String,Object> data = new HashMap<String, Object>();
          data.put("eventName","show");
          data.put("height",bottom - rect.bottom);
          ImUtils.dispatch("keyBoardEvent",data);
        }
      }
    });
  }

 

android监听软键盘事件并获取键盘高度

标签:turn   col   pre   软键盘   ldl   监听   sum   void   事件   

原文地址:https://www.cnblogs.com/winvine/p/8428225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!