Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

軟鍵盤一彈就把界面頂飛?自定義 SoftInputAdjustLayout 一次解決

編輯:關於 Android 程式設計

做 Android 登入頁、評論頁時,最常見的煩惱莫過於:輸入框明明在屏幕底部,軟鍵盤一彈出來,要嘛輸入框被擋住看不見,要嘛整個佈局被頂得往上竄。本文把這個老問題梳理清楚,並給出一個可以直接複用的自定義容器方案。

核心思路是:寫一個繼承 RelativeLayout 的容器 SoftInputAdjustLayout,把它當作頁面最外層佈局,在 onMeasure 階段實時計算鍵盤高度,再動態調整目標 view 的 bottomMargin,讓它始終浮在鍵盤之上。

//1、此 layout 作為最外層的 layout
//2、設置需要調整的 view: setAdjustView(View view);
//3、如果需要控制輸入框的顯示與隱藏,可以實現 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();
                }
            }
        });
    }

    /**
     * 需要調整的 view
     *
     * @param adjustView
     */
    public void setAdjustView(View view) {
        this.adjustView = view;
    }

    @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(…) 設置全屏;

2、在 AndroidManifest.xml 中為該 Activity 配置 android:windowSoftInputMode="adjustResize";

3、這個容器必須放在佈局的最外層,否則測高邏輯取不到正確的可用區域;

4、佈局裡一定要額外放一個尺寸為 0、可獲得焦點的 view:

<!--此 view 用於獲取焦點,當軟鍵盤彈出時,根據此 view 來彈出,
     而此 view 在最頂部,且大小為 0,所以軟鍵盤彈出,不會把背景頂上去-->
<View
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

5、調用 setAdjustView(View view) 指定需要跟著鍵盤上移的 view;

6、全屏模式下,輸入框初始狀態應為隱藏,等鍵盤真正彈出後再顯示。原因很簡單:如果輸入框一開始就擺在底部,系統會依據它來計算彈出位置,反而更容易把整層 view 頂上去。這個「先藏後露」的時序,正好交給 OnInputViewVisibleListener 來控制。

非全屏模式下的注意事項

1、在 AndroidManifest.xml 中為該 Activity 配置 android:windowSoftInputMode="adjustResize";

2、同樣要求此容器位於佈局最外層;

3、調用 setAdjustView(View view) 指定跟手/view;

4、若需要控制輸入框的顯示與隱藏,實現 OnInputViewVisibleListener 即可。

以上就是關於 Android 軟鍵盤彈出問題的整理與解法,希望對你有所幫助。如果有任何疑問,歡迎留言討論。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved