解決全屏下中輸入框被鍵盤遮擋
最近項目遇到了中使用輸入框,但是輸入框被遮擋的問題,于是上網百度了一下,發現大多數的解決方式無非兩種,一種是代碼編輯,另一種是配置清單文件。
1、在所在的中加入
().(..SIZE|..DEN);
2、調整.xml
找到對應的,加入以下屬性
:=""
若此時還有全屏屬性:theme="@:style/Theme.."評論輸入框測試點,則刪除全屏屬性。
原因:軟鍵盤彈出時,要對主窗口布局重新進行布局,并調用方法,當設置為全屏模式,官方有說明,會忽略輸入框調整。
3.布局xml調整
控件所在的布局,祖先節點不能有。另外,根節點不能固定高度。還有,當根節點是時,本身、
父節點不能固定高度。
當然上述辦法本人都試過評論輸入框測試點,但是是無效的,主要是因為我的項目是需要全屏設置的,所以是不能按照方法2那樣直接刪除全屏設置,于是乎,便繼續搜索到
了另一種方法。方法如下:
/**
* 解決webView鍵盤遮擋問題的類
* Created by zqy on 2016/11/14.

*/
public class KeyBoardListener {
private Activity activity;
// private Handler mhanHandler;
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private static KeyBoardListener keyBoardListener;
public static KeyBoardListener getInstance(Activity activity) {
// if(keyBoardListener==null){
keyBoardListener=new KeyBoardListener(activity);

// }
return keyBoardListener;
}
public KeyBoardListener(Activity activity) {
super();
// TODO Auto-generated constructor stub
this.activity = activity;
// this.mhanHandler = handler;
}
public void init() {

FrameLayout content = (FrameLayout) activity
.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent
.getLayoutParams();
}

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
frameLayoutParams.height = usableHeightSansKeyboard
- heightDifference;
} 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);
}
// private void showLog(String title, String msg) {
// Log.d("Unity", title + "------------>" + msg);
// }
}
調用方式為:KeyBoardListener.getInstance(this).init();,即可解決全屏下,鍵盤遮擋問題。