/*
* 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失效的問題
* 造成彈出的輸入法遮擋底部輸入框,或者彈出的輸入法遮擋底部內(nèi)容(原因為adjustResize失效 底部區(qū)域不會收起)
* (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的本質(zhì)其實就是狀態(tài)欄的高度且在為-1的時候才賦值。但在華為系統(tǒng)手機上發(fā)現(xiàn)切home再回來后。heightDiffDp首次的計算不正確
* 會被賦上錯誤的值。索性就直接調(diào)用getStatusBarHeight給initialDpDiff賦值算了。但getStatusBarHeight的值來源是反射的系統(tǒng)的常量值。
* 這樣三星設備又不行。唉~。只能采用下面的方式。誰值小用誰。三星設備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)
//魅族手機輸入內(nèi)容的時間高度會再次變化 因此加入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失效的問題
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內(nèi)容
- 【蝴蝶效應】 蝴蝶效應:上個世紀70年代,美國一個名叫洛倫茲的氣象學家在解釋空氣系統(tǒng)理論時說,亞馬遜雨林一只蝴蝶...