Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中監聽軟鍵盤顯示狀態實現代碼

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

編輯:關於Android編程

/**監聽軟鍵盤狀態
   * @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;
      }
    });
  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved