首頁 >Java >java教程 >Android中監聽軟鍵盤顯示狀態實現代碼

Android中監聽軟鍵盤顯示狀態實現代碼

高洛峰
高洛峰原創
2017-01-07 11:58:14971瀏覽

/**监听软键盘状态
   * @param activity
   * @param listener
   */
  public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) {
    final View decorView = activity.getWindow().getDecorView();
    decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public 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;
          
        Log.d(TAG, "DecorView display hight = " + displayHight);
        Log.d(TAG, "DecorView hight = " + hight);
        Log.d(TAG, "softkeyboard visible = " + visible);
  
        if(visible != sLastVisiable){
          listener.onSoftKeyBoardVisible(visible);
        }
        sLastVisiable = visible;
      }
    });
  }


更多Android中監聽軟體鍵盤顯示狀態實現代碼相關文章請關注PHP中文網!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn