2011-05-15 66 views
15

如何獲取Android中虛擬鍵盤的高度?可能嗎?獲取Android中虛擬鍵盤的高度

我試圖從主窗口獲取它,但它給了我應用程序的全部高度。但我想要獲得鍵盤高度。

+0

軟鍵盤是不是唯一的系統提供的裝飾,它可以使用的屏幕空間。你打算如何處理這個尺寸? – adamp 2011-05-15 16:18:27

+0

我想在軟虛擬鍵盤的頂部繪製一些圖像。我開發2D應用程序,所以我不使用Android UI庫。例如,我想繪製軟鍵盤的邊框頂部 – Adem 2011-05-16 16:32:21

+1

虛擬鍵盤不會向上推窗口嗎?在這種情況下,您只需要在屏幕底部劃一條線來繪製頂部邊框(在軟鍵盤被激活之前設置爲不可見) – keyser 2011-05-18 15:01:42

回答

4

您無法獲得鍵盤高度,但是您可以獲得視圖的高度,這正是您真正想要的 - 並且您會將此數據提供給o​​nLayout調用進入當前視圖。

+4

我的看法給了設備的全部高度。所以,這個信息不會給虛擬鍵盤高度。但是,我找到了一種方法來獲得它。在我請求打開虛擬鍵盤之後,我發送了我生成的指針事件。它們的y座標從設備的高度開始並減小。並且,在將這些事件發送給系統後,當虛擬鍵盤上的指針事件發生時,系統會拋出異常。如果這些指針事件不在虛擬鍵盤上,則沒有例外。所以我可以得到虛擬鍵盤的高度。 – Adem 2011-06-17 05:50:49

+0

它實際上是Android模型與導航欄? – 2017-06-19 11:55:31

1

您可以使用此示例代碼。它是骯髒的解決方案,但它的工作

Thread t = new Thread(){ 
      public void run() { 
       int y = mainScreenView.getHeight()-2; 
       int x = 10; 
       int counter = 0; 
       int height = y; 
       while (true){ 
        final MotionEvent m = MotionEvent.obtain(
          SystemClock.uptimeMillis(), 
          SystemClock.uptimeMillis(), 
          MotionEvent.ACTION_DOWN, 
          x, 
          y, 
          INTERNAL_POINTER_META_STATE); 
        final MotionEvent m1 = MotionEvent.obtain(
          SystemClock.uptimeMillis(), 
          SystemClock.uptimeMillis(), 
          MotionEvent.ACTION_UP, 
          x, 
          y, 
          INTERNAL_POINTER_META_STATE); 
        boolean pointer_on_softkeyboard = false; 
        try { 
         getSingletonInstrumentation().sendPointerSync(m); 
         getSingletonInstrumentation().sendPointerSync(m1); 
        } catch (SecurityException e) { 
         pointer_on_softkeyboard = true; 
        } 
        if (!pointer_on_softkeyboard){ 
         if (y == height){ 
          if (counter++ < 100){ 
           Thread.yield(); 
           continue; 
          } 
         } else if (y > 0){ 
          softkeyboard_height = mainScreenView.getHeight() - y; 
         } 
         break; 
        } 
        y--; 

       } 
       if (softkeyboard_height > 0){ 
        // it is calculated and saved in softkeyboard_height 
       } else { 
        calculated_keyboard_height = false; 
       } 
      } 
     }; 
     t.start(); 
+0

INTERNAL_POINTER_META_STATE在哪裏? metaState被引用到KeyEvent API:http://developer.android.com/reference/android/view/KeyEvent.html#getMetaState()但無法找到INTERNAL_POINTER_META_STATE。 – lordhong 2012-05-01 15:48:02

+0

INTERNAL_POINTER_META_STATE只是標識此事件源的標識。因此您可以跟蹤由您或設備生成的指針事件。你應該設置一個整數值爲INTERNAL_POINTER_META_STATE – Adem 2012-05-03 14:55:41

+4

什麼是getSingletonInstrumentation()方法? – yahya 2013-01-07 12:10:03

1

此解決方案也hacky,但解決問題(至少對我來說)。

  1. 我在屏幕底部有透明背景的臨時視圖。所以這個觀點將是不可見的。
  2. 我在清單中的活動標記中添加了android:windowSoftInputMode="adjustResize"標誌。
  3. 現在的主要故事在onGlobalLayout()。在那裏,我計算了臨時視圖的y軸和根視圖的高度之間的差異。

    final View view = findViewById(R.id.base); 。 view.getViewTreeObserver()addOnGlobalLayoutListener(新OnGlobalLayoutListener(){

    @Override 
    public void onGlobalLayout() { 
    
        int rootViewHeight = view.getRootView().getHeight(); 
        View tv = findViewById(R.id.temp_view); 
        int location[] = new int[2]; 
        tv.getLocationOnScreen(location); 
        int height = (int) (location[1] + tv.getMeasuredHeight()); 
        deff = rootViewHeight - height; 
        // deff is the height of soft keyboard 
    
    } 
    

    });

+0

不錯的把戲兄弟。 – kobbycoder 2015-11-15 03:26:11

0

試試這個

KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null); 
int height = (keyboardView.getKeyboard()).getHeight(); 
Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show(); 
+1

看起來很簡單,只是兩行代碼...但它不適合我'keyboardView.getKeyboard()''null'' – Dirk 2014-10-25 20:55:17

+0

我也一樣..keyboardView.getKeyboard()返回null – 2014-11-04 08:33:37

+0

是的。 ..不工作:( – Georg 2015-05-26 11:57:50

1

如果你不想在你的應用程序android:windowSoftInputMode="adjustResize"做。 你可以嘗試這樣的事情:

any_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      int height = main_layout.getHeight(); 
      Log.w("Foo", String.format("layout height: %d", height)); 
      Rect r = new Rect(); 
      main_layout.getWindowVisibleDisplayFrame(r); 
      int visible = r.bottom - r.top; 
      Log.w("Foo", String.format("visible height: %d", visible)); 
      Log.w("Foo", String.format("keyboard height: %d", height - visible)); 
     } 
    }); 
+1

getWindowVisibleDisplayFrame不適合我..它返回視圖的高度 – 2015-08-20 08:41:47