作者:ADark0915
出处:http://www.cnblogs.com/shelly-li/
首先,在manifest文件中声明此Activity的windowSoftInputMode属性,
android:windowSoftInputMode="stateVisible|adjustResize"
在Activity中 声明所需用到的变量以及布局的事件监听,
private boolean mBackEnable = false;
private boolean mIsBtnBack = false;
private int rootBottom = Integer.MIN_VALUE;
private OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mSearchLayout.getGlobalVisibleRect(r);
// 进入Activity时会布局,第一次调用onGlobalLayout,先记录开始软键盘没有弹出时底部的位置
if (rootBottom == Integer.MIN_VALUE) {
rootBottom = r.bottom;
return;
}
// adjustResize,软键盘弹出后高度会变小
if (r.bottom < rootBottom) {
mBackEnable = false;
} else {
mBackEnable = true;
if (mIsBtnBack) {
finish();
}
}
}
};
然后在onCreat方法中去注册事件监听
mSearchLayout.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
注意:这个mSearchlayout是你整个Activity的布局引用
最后,别忘了,在onDestroy方法中回收掉事件的监听
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onDestroy() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
mSearchLayout.getViewTreeObserver().removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
} else {
mSearchLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
}
super.onDestroy();
}
到这里,android软键盘的监听就结束了。
什么?你说全屏的时候,没有效果???
好吧,我也遇到了。。。
我在项目中实现了所有界面的沉浸式效果,本以为Activity设置了adjustResize,OnGlobalLayoutListener仍然能监听软键盘的变化,结果却是屡试不行。。。
无奈,只能求助。查了一些资料,发现这原来是android的一个bug。下面是具体的bug修复代码片段
public class AndroidAdjustResizeBugFix {
private View mChildOfContent;
private int usableHeightPrevious;
private int statusBarHeight;
private FrameLayout.LayoutParams frameLayoutParams;
private Activity mActivity;
private AndroidAdjustResizeBugFix(Activity activity) {
mActivity = activity;
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
statusBarHeight = getStatusBarHeight();
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
public static void assistActivity(Activity activity) {
new AndroidAdjustResizeBugFix(activity);
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// keyboard probably just became visible
// 如果有高度变化,mChildOfContent.requestLayout()之后界面才会重新测量
// 这里就随便让原来的高度减去了1
frameLayoutParams.height = usableHeightSansKeyboard - 1;
键盘开启
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
键盘关闭
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return r.bottom - r.top + statusBarHeight;
}
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
int dimensionPixelSize = mActivity.getResources().getDimensionPixelSize(x);
return dimensionPixelSize;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
把这个类作为一个工具类,添加到项目中,然后,在需要做优化的Activity的onCreate方法中注册activity即可,如:
AndroidAdjustResizeBugFix.assistActivity(this);
以上,就是关于监听软键盘弹出和收起的所有内容。
网友评论