android键盘监听

//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;    
                }            }});

猜你喜欢

转载自blog.csdn.net/u010505059/article/details/80693081
今日推荐