天天看點

Android虛拟鍵盤的高度計算

系統本身沒有提供擷取虛拟鍵盤的方法,在網上查了一些資料,發現還存在一點問題,故此把改好的代碼記錄下來,以備後用。

需要用OnGlobalLayoutListener來監聽app視窗的變化

final View decorView = getActivity().getWindow().getDecorView();
        //虛拟按鍵高度
        final int vmKeyHeight = getActivity().getResources().getDimensionPixelSize(getResources().getIdentifier("navigation_bar_height", "dimen", "android"));

        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                int displayHeight = rect.bottom - rect.top;//可見螢幕的高度,不包括頂部狀态欄和底部虛拟按鍵
                int keyboardHeight = decorView.getHeight() - displayHeight - rect.top;//這裡要減去頂部狀态欄高度,否則會不準,看網上的文章大都沒減去狀态欄高度
                if(isVmKeyShow())//如果顯示虛拟按鍵,則還要減去虛拟按鍵的高度
                    keyboardHeight-=vmKeyHeight;//這個就是最終虛拟鍵盤的高度,後面是資料的使用
                }
            }
        });
           
private boolean isVmKeyShow() {//這個方法在三星手機上測試不好用
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size=new Point();
        Point realSize=new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return size.y!=realSize.y;
    }
           
上面判斷虛拟按鍵的方式在三星手機上測試不好用,故此改了整體代碼,目的是切換輸入法及虛拟按鍵,保持輸入框位置不變,如下
final View decorView = getActivity().getWindow().getDecorView();
        //擷取虛拟按鍵高度
        int id = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        final int vmKeyHeight =id==0? 0: getActivity().getResources().getDimensionPixelSize(id);

        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                int displayHeight = rect.bottom - rect.top;//可見螢幕的高度,不包括頂部狀态欄和底部虛拟按鍵
                int keyboardHeight = decorView.getHeight() - displayHeight - rect.top;//包括虛拟按鍵
                int bottomHeight = EdoPreference.getInt(EdoPreference.KEY_KEYBOARD_HEIGHT, 0);
                if(rect.bottom<decorView.getHeight()){//虛拟按鍵顯示
                    if(keyboardHeight>vmKeyHeight){//鍵盤處于顯示狀态
                        keyboardHeight-=vmKeyHeight;
                        if(keyboardHeight==bottomHeight){
                            isKeyboardLastShow=true;
                            return;
                        }
                        bottomHeight=keyboardHeight;
                    }else {//隻有虛拟按鍵顯示,isKeyboardLastShow是boolean全局變量
                        if(!isKeyboardLastShow)
                            bottomHeight-=vmKeyHeight;
                    }
                    isKeyboardLastShow=true;
                }else{
                    if(isKeyboardLastShow)
                        bottomHeight+=vmKeyHeight;
                    isKeyboardLastShow=false;
                }
                if (1.0 * bottomHeight / displayHeight > 0.4) {
                    chatBottomView.setContentViewHeight(bottomHeight);
                    EdoPreference.setPref(EdoPreference.KEY_KEYBOARD_HEIGHT, bottomHeight);
                }
            }
        });
           

繼續閱讀