fullScreen时的软键盘监听(非重写Layout方式)
16lz
2021-01-23
应用场景:
manifest中activity设置如下属性:
android:windowSoftInputMode="stateHidden"android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
此时按照网上搜索的通常方法,重写linearLayout,会不起作用,因为layout的bottom属性在全屏中不变。
改用api 1就有ViewTreeObserver来监听layout变化:
//获取最外层Layoutfinal View rootView=((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() { Rect rect = new Rect();rootView.getWindowVisibleDisplayFrame(rect);int rootHeight=rootView.getRootView().getHeight();int displayHeight=rect.height(); int diffHeight=rootHeight-displayHeight;if(diffHeight==0){ //键盘收起 }else{ //键盘弹出 } }});
PS:渣排版,轻拍