問題截圖
bug.png
如上,我紅色指著的方向是一個輸入框坐桩,它被擋住了瘟斜。我整個灰色的是popuwindow
參考的網(wǎng)站
代碼實現(xiàn)
//屏幕高度
int screenHeight = 0;
public interface OnSoftKeyboardStateChangedListener {
public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight);
}
//注冊軟鍵盤狀態(tài)變化監(jiān)聽
public void addSoftKeyboardChangedListener(SchedulingActivity.OnSoftKeyboardStateChangedListener listener) {
if (listener != null) {
mKeyboardStateListeners.add(listener);
}
}
//取消軟鍵盤狀態(tài)變化監(jiān)聽
public void removeSoftKeyboardChangedListener(SchedulingActivity.OnSoftKeyboardStateChangedListener listener) {
if (listener != null) {
mKeyboardStateListeners.remove(listener);
}
}
private ArrayList<SchedulingActivity.OnSoftKeyboardStateChangedListener> mKeyboardStateListeners; //軟鍵盤狀態(tài)監(jiān)聽列表
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() {
//判斷窗口可見區(qū)域大小
Rect r = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//如果屏幕高度和Window可見區(qū)域高度差值大于整個屏幕高度的1/3冬三,則表示軟鍵盤顯示中,否則軟鍵盤為隱藏狀態(tài)燎字。
int heightDifference = screenHeight - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > screenHeight / 3;
//如果之前軟鍵盤狀態(tài)為顯示腥椒,現(xiàn)在為關(guān)閉阿宅,或者之前為關(guān)閉,現(xiàn)在為顯示笼蛛,則表示軟鍵盤的狀態(tài)發(fā)生了改變
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);
}
}
}
};
//注冊布局變化監(jiān)聽
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) {//軟鍵盤顯示洒放,這里設(shè)置了控制底部多少距離,各位可以在這里對界面進(jìn)行控制
int paddingBottom = DensityUtil.dip2px(BAFApplication.getApplication(), linearLayoutPopuMenuParent.getPaddingBottom() + 30);
linearLayoutPopuMenuParent.setPadding(linearLayoutPopuMenuParent.getPaddingLeft(), linearLayoutPopuMenuParent.getPaddingTop(), linearLayoutPopuMenuParent.getPaddingRight(), paddingBottom);
} else {//軟鍵盤沒有顯示滨砍,當(dāng)軟鍵盤沒有顯示的時候拉馋,就要還原
linearLayoutPopuMenuParent.setPadding(linearLayoutPopuMenuParent.getPaddingLeft(), linearLayoutPopuMenuParent.getPaddingTop(), linearLayoutPopuMenuParent.getPaddingRight(), 0);
}
}
});
}
}
protected void onDestroy() {
//移除布局變化監(jiān)聽
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;//返回一個實例
}
/**
* 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根據(jù)手機(jī)的分辨率從 px(像素) 的單位 轉(zhuǎn)成為 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
fixbug.png
優(yōu)化 2019-10-12
將工具方法抽離出來
使用教程
KeyBordChangeUtils keyBordChangeUtils = new KeyBordChangeUtils(this.getActivity());
if (keyBordChangeUtils!=null){
//注冊監(jiān)聽
keyBordChangeUtils.addSoftKeyboardChangedListener(new KeyBordChangeUtils.OnSoftKeyboardStateChangedListener() {
@Override
public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight) {
if (productInfoPopuwindow==null){
return;
}
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) productInfoPopuwindow.getPopuwindowLinlayoutParent().getLayoutParams();
if (isKeyBoardShow) {//軟鍵盤顯示菲饼,這里設(shè)置了控制底部多少距離
layoutParams.bottomMargin = keyboardHeight +DensityUtil.getInstance().dip2px(10);
productInfoPopuwindow.getPopuwindowLinlayoutParent().setLayoutParams(layoutParams);
} else {//軟鍵盤沒有顯示待讳,當(dāng)軟鍵盤沒有顯示的時候,就要還原
layoutParams.bottomMargin = 0;
productInfoPopuwindow.getPopuwindowLinlayoutParent().setLayoutParams(layoutParams);
}
}
});
}
注意點:
keyboardHeight:包含了狀態(tài)欄的高度
/**
* @date :2019/10/12 0012
* @author : gaoxiaoxiong
* @description:軟鍵盤監(jiān)聽
**/
public class KeyBordChangeUtils {
private WeakReference<Activity> weakReference;
int screenHeight = 0;
public interface OnSoftKeyboardStateChangedListener {
void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight);
}
//注冊軟鍵盤狀態(tài)變化監(jiān)聽
public void addSoftKeyboardChangedListener(OnSoftKeyboardStateChangedListener listener) {
if (listener != null) {
mKeyboardStateListeners.add(listener);
}
}
//取消軟鍵盤狀態(tài)變化監(jiān)聽
public void removeSoftKeyboardChangedListener(OnSoftKeyboardStateChangedListener listener) {
if (listener != null) {
mKeyboardStateListeners.remove(listener);
}
}
private ArrayList<OnSoftKeyboardStateChangedListener> mKeyboardStateListeners; //軟鍵盤狀態(tài)監(jiān)聽列表
private ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener;
private boolean mIsSoftKeyboardShowing;
public KeyBordChangeUtils(Activity activity) {
if (weakReference == null){
weakReference = new WeakReference<Activity>(activity);
}
init();
}
private void init(){
//監(jiān)聽軟鍵盤
WindowManager wm = (WindowManager) weakReference.get().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
screenHeight = dm.heightPixels; // 屏幕高度(像素)
//屏幕高度
mIsSoftKeyboardShowing = false;
mKeyboardStateListeners = new ArrayList<OnSoftKeyboardStateChangedListener>();
mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//判斷窗口可見區(qū)域大小
Rect r = new Rect();
weakReference.get().getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//如果屏幕高度和Window可見區(qū)域高度差值大于整個屏幕高度的1/3双妨,則表示軟鍵盤顯示中日川,否則軟鍵盤為隱藏狀態(tài)蔓腐。
int heightDifference = screenHeight - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > screenHeight / 3;
//如果之前軟鍵盤狀態(tài)為顯示,現(xiàn)在為關(guān)閉龄句,或者之前為關(guān)閉回论,現(xiàn)在為顯示,則表示軟鍵盤的狀態(tài)發(fā)生了改變
if ((mIsSoftKeyboardShowing && !isKeyboardShowing) || (!mIsSoftKeyboardShowing && isKeyboardShowing)) {
mIsSoftKeyboardShowing = isKeyboardShowing;
//這里是回調(diào)分歇,通知軟鍵盤已經(jīng)發(fā)生了變化
for (int i = 0; i < mKeyboardStateListeners.size(); i++) {
OnSoftKeyboardStateChangedListener listener = mKeyboardStateListeners.get(i);
listener.OnSoftKeyboardStateChanged(mIsSoftKeyboardShowing, heightDifference);
}
}
}
};
//注冊布局變化監(jiān)聽
weakReference.get().getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
}
/**
* @date :2019/10/12 0012
* @author : gaoxiaoxiong
* @description:銷毀
**/
public void onDestory(){
if (mKeyboardStateListeners!=null){
mKeyboardStateListeners.clear();
}
if (weakReference!=null){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
weakReference.get().getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutChangeListener);
} else {
weakReference.get().getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(mLayoutChangeListener);
}
}
}
}