2015-03-05 3 views
3

Я пытаюсь получить андроид клавиатуру Высоты с помощью следующего кодаПолучить Android Keyboard Рост

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", тогда я не могу получить высоту клавиатуры. Мне нужно альтернативное решение для моей проблемы. Advance Спасибо!

ответ

0

Я использую этот метод для известково высоты клавиатуры:

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

Он всегда дает 0 для высоты клавиатуры.! –

+0

Я предоставляю родительский макет деятельности как viewMain. –

+0

Запустить в потоке этот метод, когда onClick на вашем EditText –

-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); 
    } 
} 
+0

Я не хочу скрывать клавиатуру, я хочу получить размер клавиатуры. –