/*
* File Name: KeyboardUtil.java
* History:
* Created by mulinrui on 2015年8月26日
*/
package com.zhiyoo.util;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import com.anzhi.common.util.VersionUtils;
import com.zhiyoo.ui.MarketBaseActivity;
/**
* 解决当activity全屏时 使用adjustResize失效的问题
* 造成弹出的输入法遮挡底部输入框,或者弹出的输入法遮挡底部内容(原因为adjustResize失效 底部区域不会收起)
* (Description)
*
* @author mulinrui
*/
public class KeyboardUtil {
private View decorView;
private View contentView;
private float initialDpDiff = -1;
private static KeyboardUtil sInstance;
private KeyboardUtil() {
}
public static KeyboardUtil getInstance() {
if (null == sInstance) {
sInstance = new KeyboardUtil();
}
return sInstance;
}
public void enable(View contentView, MarketBaseActivity act) {
if (VersionUtils.hasKitKat() && contentView != null && act != null) {
this.decorView = act.getWindow().getDecorView();
this.contentView = contentView;
this.initialDpDiff = -1;
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
public void disable() {
if (VersionUtils.hasKitKat() && decorView != null) {
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
// ==========================================================================
// Inner/Nested Classes
// ==========================================================================
// a small helper to allow showing the editText focus
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
// r will be populated with the coordinates of your view that area still visible.
decorView.getWindowVisibleDisplayFrame(r);
int usableHeightSansKeyboard = decorView.getRootView().getHeight();
// get the height diff as dp
float heightDiffDp = usableHeightSansKeyboard - (r.bottom - r.top);
// set the initialDpDiff at the beginning. (on my phone this was 73dp)
if (initialDpDiff == -1 && heightDiffDp <= usableHeightSansKeyboard / 4) {
/**
* 一言难尽
* initialDpDiff的本质其实就是状态栏的高度且在为-1的时候才赋值。但在华为系统手机上发现切home再回来后。heightDiffDp首次的计算不正确
* 会被赋上错误的值。索性就直接调用getStatusBarHeight给initialDpDiff赋值算了。但getStatusBarHeight的值来源是反射的系统的常量值。
* 这样三星设备又不行。唉~。只能采用下面的方式。谁值小用谁。三星设备heightDiffDp为0.切Home后回来用statusBarHeight。
* liubin 2017/5/18 19:52
*/
initialDpDiff = heightDiffDp;
}
//要添加的高度
float bootomPadding = heightDiffDp - initialDpDiff;
// LogUtils.w(" OnGlobalLayoutListener bootomPadding:" + bootomPadding);
// if it could be a keyboard add the padding to the view 此处需要多进行测试
if (bootomPadding > usableHeightSansKeyboard / 4) { // if more than 100 dp, its probably a keyboard...
// check if the padding is 0 (if yes set the padding for the keyboard)
//魅族手机输入内容的时间高度会再次变化 因此加入bootomPadding != contentView.getPaddingBottom()判断
if (contentView.getPaddingBottom() == 0 || bootomPadding != contentView.getPaddingBottom()) {
// set the padding of the contentView for the keyboard
contentView.setPadding(0, 0, 0, (int) bootomPadding);
}
} else {
// check if the padding is != 0 (if yes reset the padding)
if (contentView.getPaddingBottom() != 0) {
// reset the padding of the contentView
contentView.setPadding(0, 0, 0, 0);
}
}
}
};
}
解决当activity全屏时 使用adjustResize失效的问题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...