问题截图
bug.png如上,我红色指着的方向是一个输入框,它被挡住了。我整个灰色的是popuwindow
参考的网站
代码实现
//屏幕高度
int screenHeight = 0;
public interface OnSoftKeyboardStateChangedListener {
public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight);
}
//注册软键盘状态变化监听
public void addSoftKeyboardChangedListener(SchedulingActivity.OnSoftKeyboardStateChangedListener listener) {
if (listener != null) {
mKeyboardStateListeners.add(listener);
}
}
//取消软键盘状态变化监听
public void removeSoftKeyboardChangedListener(SchedulingActivity.OnSoftKeyboardStateChangedListener listener) {
if (listener != null) {
mKeyboardStateListeners.remove(listener);
}
}
private ArrayList<SchedulingActivity.OnSoftKeyboardStateChangedListener> mKeyboardStateListeners; //软键盘状态监听列表
private ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener;
private boolean mIsSoftKeyboardShowing;
protected void onCreate(Bundle savedInstanceState) {
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
screenHeight = dm.heightPixels; // 屏幕高度(像素)
//屏幕高度
mIsSoftKeyboardShowing = false;
mKeyboardStateListeners = new ArrayList<SchedulingActivity.OnSoftKeyboardStateChangedListener>();
mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//判断窗口可见区域大小
Rect r = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
int heightDifference = screenHeight - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > screenHeight / 3;
//如果之前软键盘状态为显示,现在为关闭,或者之前为关闭,现在为显示,则表示软键盘的状态发生了改变
if ((mIsSoftKeyboardShowing && !isKeyboardShowing) || (!mIsSoftKeyboardShowing && isKeyboardShowing)) {
mIsSoftKeyboardShowing = isKeyboardShowing;
for (int i = 0; i < mKeyboardStateListeners.size(); i++) {
SchedulingActivity.OnSoftKeyboardStateChangedListener listener = mKeyboardStateListeners.get(i);
listener.OnSoftKeyboardStateChanged(mIsSoftKeyboardShowing, heightDifference);
}
}
}
};
//注册布局变化监听
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
showBottomView();
}
public void showBottomView() {
if (bottomView == null) {
addSoftKeyboardChangedListener(new SchedulingActivity.OnSoftKeyboardStateChangedListener() {
@Override
public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight) {
if (isKeyBoardShow) {//软键盘显示,这里设置了控制底部多少距离
int paddingBottom = DensityUtil.dip2px(BAFApplication.getApplication(), linearLayoutPopuMenuParent.getPaddingBottom() + 30);
linearLayoutPopuMenuParent.setPadding(linearLayoutPopuMenuParent.getPaddingLeft(), linearLayoutPopuMenuParent.getPaddingTop(), linearLayoutPopuMenuParent.getPaddingRight(), paddingBottom);
} else {//软键盘没有显示,当软键盘没有显示的时候,就要还原
linearLayoutPopuMenuParent.setPadding(linearLayoutPopuMenuParent.getPaddingLeft(), linearLayoutPopuMenuParent.getPaddingTop(), linearLayoutPopuMenuParent.getPaddingRight(), 0);
}
}
});
}
}
protected void onDestroy() {
//移除布局变化监听
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutChangeListener);
} else {
getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(mLayoutChangeListener);
}
super.onDestroy();
};
工具类
public class DensityUtil {
private static DensityUtil densityUtil;
public static DensityUtil getInstance() {
if (densityUtil == null) {
densityUtil = new DensityUtil();
}
return densityUtil;//返回一个实例
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
fixbug.png
网友评论