android键盘监听

//contentlayout是最外层布局View mChildOfContent;int usableHeightPrevious;   mChildOfContent = contentlayout.getChildAt(0);mChildOfContent.getViewTreeObserver()               .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {              public void onGlobalLayout() { //contentlayout是最外层布局View mChildOfContent;int usableHeightPrevious;   mChildOfContent = contentlayout.getChildAt(0);mChildOfContent.getViewTreeObserver()               .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            public void onGlobalLayout() {
                    Rect r = new Rect();                        mChildOfContent.getWindowVisibleDisplayFrame(r);                     int usableHeightNow = r.bottom - r.top;                     if (usableHeightNow != usableHeightPrevious) {                    int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();                    int heightDifference = usableHeightSansKeyboard - usableHeightNow;                    if (heightDifference > (usableHeightSansKeyboard / 4)) {                        // 键盘弹出                    } else {                       // 键盘收起                          }                mChildOfContent.requestLayout();                usableHeightPrevious = usableHeightNow;                 }            }});                    Rect r = new Rect();                        mChildOfContent.getWindowVisibleDisplayFrame(r);                     int usableHeightNow = r.bottom - r.top;                     if (usableHeightNow != usableHeightPrevious) {                    int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();                    int heightDifference = usableHeightSansKeyboard - usableHeightNow;                    if (heightDifference > (usableHeightSansKeyboard / 4)) {                        // 键盘弹出                    } else {                       // 键盘收起                          }                mChildOfContent.requestLayout();                usableHeightPrevious = usableHeightNow;                 }            }});