【问题标题】:Get Android Keyboard Height获取 Android 键盘高度
【发布时间】:2015-05-06 12:28:20
【问题描述】:

我正在尝试使用以下代码获取 android 键盘高度

parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    parentLayout.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parentLayout.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom);

                    previousHeightDiffrence = heightDifference;
                    if (heightDifference > 100) {
                        isKeyBoardVisible = true;
                        changeKeyboardHeight(heightDifference);


                    } else {
                        if(emojiKeyboard.getVisibility()==View.INVISIBLE){
                            emojiKeyboard.setVisibility(View.GONE);
                        }

                        isKeyBoardVisible = false;
                    }

                }
            });

这适用于android:windowSoftInputMode="adjustPan",但这会导致我的活动屏幕在默认键盘出现时向上移动。 我需要的只是获取键盘高度并在默认键盘下显示我的自定义键盘,据我说只有android:windowSoftInputMode="adjustPan" 或android:windowSoftInputMode="AdjustNothing" 才有可能。 如果设置android:windowSoftInputMode="adjustNothing" 那么我无法获得键盘高度。 我需要替代解决方案来解决我的问题。 提前谢谢!

【问题讨论】:

    标签: android android-layout keyboard android-input-method


    【解决方案1】:

    我用这个方法来计算键盘高度:

    public static int getKeyboardHeight() {
     // viewMain <-- findViewById( android.R.id.content).getRootView();
    
     if( viewMain != null)  iSoftkeyboardHeight = viewMain.getHeight();
    
     int y = iSoftkeyboardHeight - 2;
     int x = 10;
     int counter = 0;
     int height = y;
     int iSoftkeyboardHeightNow = 0;
     Instrumentation instrumentation = new Instrumentation();
     while( true) {
        final MotionEvent m = MotionEvent.obtain(  SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0);
        final MotionEvent m1 = MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,   x, y, 0);
        boolean ePointerOnSoftkeyboard = false;
    
        try {
            instrumentation.sendPointerSync(m);
            instrumentation.sendPointerSync(m1);
          }
         catch (SecurityException e) {
             ePointerOnSoftkeyboard = true;
          }
        if( !ePointerOnSoftkeyboard) {
            if( y == height) {
                if( counter++ < 100) {
                    Thread.yield();
                    continue;
                  }
              }
             else
              if( y > 0)
                iSoftkeyboardHeightNow = iSoftkeyboardHeight - y;
            break;
          }
        y--;
        m.recycle();
        m1.recycle();
      }
     if( iSoftkeyboardHeightNow > 0)  iSoftkeyboardHeight = iSoftkeyboardHeightNow;
      else iSoftkeyboardHeight = 0;
     return iSoftkeyboardHeight;
    

    }

    【讨论】:

    • 键盘高度总是为0!
    • 我将活动的父布局作为 viewMain。
    【解决方案2】:

    试试这个代码 -

    public static void hideSoftKeyboard(EditText editText) {
        if (null != editText) {
            InputMethodManager imm = (InputMethodManager) editText.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }
    

    【讨论】:

    • 我不想隐藏键盘,我想知道键盘的大小。
    【解决方案3】:
    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            parent.getWindowVisibleDisplayFrame(r);
    
            int screenHeight = parent.getRootView().getHeight();
            int heightDifference = screenHeight - (r.bottom - r.top);
            Log.d("Keyboard Size", "Size: " + heightDifference);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2016-04-25
      • 2018-08-22
      • 1970-01-01
      • 2013-08-28
      • 2016-12-26
      相关资源
      最近更新 更多