Android 彈出軟鍵盤所遇到的坑及解決方法
重要代碼:
//1、此layout作為最外層的layout;
//2、設(shè)置需要調(diào)整的view: setAdjustView(View view);
//3、如果需要控制輸入框的顯示與隱藏,可以實現(xiàn)OnInputViewVisibleListener接口;
public class SoftInputAdjustLayout extends RelativeLayout {
private static final String TAG = SoftInputAdjustLayout.class.getSimpleName();
private View adjustView;
public SoftInputAdjustLayout(Context context) {
this(context, null);
}
public SoftInputAdjustLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SoftInputAdjustLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
try {
Rect r = new Rect();
getWindowVisibleDisplayFrame(r);
int heightDiff = getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
if (onInputViewVisibleListener != null) {
onInputViewVisibleListener.onShowInputView();
}
} else {
if (onInputViewVisibleListener != null) {
onInputViewVisibleListener.onHideInputView();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 需要調(diào)整的view
*
* @param adjustView
*/
public void setAdjustView(View adjustView) {
this.adjustView = adjustView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (adjustView != null) {
Rect rect = new Rect();
getWindowVisibleDisplayFrame(rect);
int totalHeight = getHeight();
int nowHeight = rect.bottom - rect.top;
int keyBoardHeight = totalHeight - nowHeight;
if (keyBoardHeight > totalHeight / 4) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
LayoutParams params = (LayoutParams) adjustView.getLayoutParams();
params.bottomMargin = keyBoardHeight;
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
LayoutParams params = (LayoutParams) adjustView.getLayoutParams();
params.bottomMargin = 0;
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
private OnInputViewVisibleListener onInputViewVisibleListener;
public void setOnInputViewVisibleListener(OnInputViewVisibleListener listener) {
onInputViewVisibleListener = listener;
}
public interface OnInputViewVisibleListener {
void onShowInputView();
void onHideInputView();
}
}
備注:
全屏模式:
1、通過style或者setFlags(…)來設(shè)置全屏;
2、AndroidManifest.xml中activity設(shè)置:android:windowSoftInputMode=”adjustResize”;
3、此layout必須作為布局的最外層layout;
4、一定要在布局中添加一個view,如下:
<!--此view用于獲取焦點,當軟件盤彈出時,根據(jù)此view來彈出,而此view在最頂部,且大小為0,所以軟件盤彈出,不會把背景頂上去-->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
5、通過setAdjustView(View view)來設(shè)置需要調(diào)整的view;
6、在全屏模式下,輸入框開始是未顯示的,直到彈出軟鍵盤,才會顯示輸入框。因為如果輸入框一開始就顯示,那軟鍵盤就會根據(jù)輸入框來彈出,且輸入框在底部,那就可能造成把整個view頂上去。所以這時我們應該實現(xiàn)OnInputViewVisibleListener這個接口來控制輸入框的顯示與隱藏。
非全屏模式
1、AndroidManifest.xml中activity設(shè)置:android:windowSoftInputMode=”adjustResize”;
2、此layout必須作為布局的最外層layout;
3、通過setAdjustView(View view)來設(shè)置需要調(diào)整的view;
4、如果需要控制輸入框的顯示與隱藏,就實現(xiàn)OnInputViewVisibleListener這個接口。
以上所述是小編給大家介紹的Android 彈出軟鍵盤所遇到的坑及解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android App的運行環(huán)境及Android系統(tǒng)架構(gòu)概覽
這篇文章主要介紹了Android App的運行環(huán)境及Android系統(tǒng)架構(gòu)概覽,并對應用程序進程間隔離機制等知識點作了介紹,需要的朋友可以參考下2016-03-03
Android HorizontalScrollView內(nèi)子控件橫向拖拽實例代碼
本文主要介紹Android HorizontalScrollView的使用,這里給大家一個實例來展示HorizontalScrollView內(nèi)子控件橫向拖拽的效果實現(xiàn),有需要的小伙伴可以參考下2016-07-07
Android教你如何發(fā)現(xiàn)APP卡頓的實現(xiàn)
這篇文章主要介紹了Android教你如何發(fā)現(xiàn)APP卡頓的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Android實現(xiàn)帶指示點的自動輪播無限循環(huán)效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)帶指示點的自動輪播無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

