Android 彈出軟鍵盤所遇到的坑及解決方法
重要代碼:
//1、此layout作為最外層的layout;
//2、設(shè)置需要調(diào)整的view: setAdjustView(View view);
//3、如果需要控制輸入框的顯示與隱藏,可以實(shí)現(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、一定要在布局中添加一個(gè)view,如下:
<!--此view用于獲取焦點(diǎn),當(dāng)軟件盤彈出時(shí),根據(jù)此view來彈出,而此view在最頂部,且大小為0,所以軟件盤彈出,不會(huì)把背景頂上去-->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
5、通過setAdjustView(View view)來設(shè)置需要調(diào)整的view;
6、在全屏模式下,輸入框開始是未顯示的,直到彈出軟鍵盤,才會(huì)顯示輸入框。因?yàn)槿绻斎肟蛞婚_始就顯示,那軟鍵盤就會(huì)根據(jù)輸入框來彈出,且輸入框在底部,那就可能造成把整個(gè)view頂上去。所以這時(shí)我們應(yīng)該實(shí)現(xiàn)OnInputViewVisibleListener這個(gè)接口來控制輸入框的顯示與隱藏。
非全屏模式
1、AndroidManifest.xml中activity設(shè)置:android:windowSoftInputMode=”adjustResize”;
2、此layout必須作為布局的最外層layout;
3、通過setAdjustView(View view)來設(shè)置需要調(diào)整的view;
4、如果需要控制輸入框的顯示與隱藏,就實(shí)現(xiàn)OnInputViewVisibleListener這個(gè)接口。
以上所述是小編給大家介紹的Android 彈出軟鍵盤所遇到的坑及解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽
這篇文章主要介紹了Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽,并對(duì)應(yīng)用程序進(jìn)程間隔離機(jī)制等知識(shí)點(diǎn)作了介紹,需要的朋友可以參考下2016-03-03
Android HorizontalScrollView內(nèi)子控件橫向拖拽實(shí)例代碼
本文主要介紹Android HorizontalScrollView的使用,這里給大家一個(gè)實(shí)例來展示HorizontalScrollView內(nèi)子控件橫向拖拽的效果實(shí)現(xiàn),有需要的小伙伴可以參考下2016-07-07
Android教你如何發(fā)現(xiàn)APP卡頓的實(shí)現(xiàn)
這篇文章主要介紹了Android教你如何發(fā)現(xiàn)APP卡頓的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android實(shí)現(xiàn)帶指示點(diǎn)的自動(dòng)輪播無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶指示點(diǎn)的自動(dòng)輪播無限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

