1. 程式人生 > 實用技巧 >android軟鍵盤彈出時如何處理頁面佈局

android軟鍵盤彈出時如何處理頁面佈局

android裡軟鍵盤彈出頂起當前佈局是常見的問題。

首先了解下軟鍵盤和activity的幾個互動模式(windowSoftInputMode)

【A】stateUnspecified:軟鍵盤的狀態並沒有指定,系統將選擇一個合適的狀態或依賴於主題的設定

【B】stateUnchanged:當這個activity出現時,軟鍵盤將一直保持在上一個activity裡的狀態,無論是隱藏還是顯示

【C】stateHidden:使用者選擇activity時,軟鍵盤總是被隱藏

【D】stateAlwaysHidden:當該Activity主視窗獲取焦點時,軟鍵盤也總是被隱藏的

【E】stateVisible:軟鍵盤通常是可見的

【F】stateAlwaysVisible:使用者選擇activity時,軟鍵盤總是顯示的狀態

【G】adjustUnspecified:預設設定,通常由系統自行決定是隱藏還是顯示。使用該設定,軟鍵盤彈出,頁面裡的圖片可能會有影響。

【H】adjustResize:該Activity總是調整螢幕的大小以便留出軟鍵盤的空間

【I】adjustPan:當前視窗的內容將自動移動以便當前焦點從不被鍵盤覆蓋和使用者能總是看到輸入內容的部分

網上常見的解決問題的方案是將windowSoftInputMode 的值設定為

android:windowSoftInputMode="adjustResize|stateHidden"

或者

android:windowSoftInputMode="adjustPan|stateHidden"

但是,當頁面比較複雜時,軟鍵盤依然會破壞當前佈局,或遮擋輸入框。

下面從配置到程式碼梳理下解決方案。

本例中,一個頁面包含10+各edittext,都是通過程式碼動態新增的

1,設定AndroidManifest.xml裡的windowSoftInputMode

<activity
            
android:name=".view.PostActivity" android:configChanges="keyboardHidden" android:windowSoftInputMode="adjustResize|stateHidden" />

2,軟鍵盤監聽

public class KeyboardListener {
    final String TAG=this.getClass().getName();

    private View rootView; // activity的根檢視
    int rootViewVisibleHeight; //
記錄根檢視顯示的高度 private OnKeyBoardDisplayListener onKeyBoardDisplayListener; public KeyboardListener(Activity activity){ // 獲取activity的根檢視 rootView = activity.getWindow().getDecorView(); // 監聽檢視樹中全域性佈局發生改變或者檢視樹中某個檢視的可視狀態發生改變 rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 獲取當前根檢視在螢幕上顯示的大小 Rect rect = new Rect(); rootView.getWindowVisibleDisplayFrame(rect); int visibleHeight = rect.height(); if(rootViewVisibleHeight == 0){ rootViewVisibleHeight = visibleHeight; return; } //根檢視顯示高度沒有變化,可以看做軟鍵盤顯示/隱藏狀態沒有變化 if(rootViewVisibleHeight == visibleHeight){ return; } // 根檢視顯示高度變小超過200,可以看做軟鍵盤顯示了 if(rootViewVisibleHeight -visibleHeight > 200){ if(onKeyBoardDisplayListener !=null){ onKeyBoardDisplayListener.keyBoardShow(rootViewVisibleHeight - visibleHeight); } rootViewVisibleHeight = visibleHeight; return; } // 根檢視顯示高度變大超過了200,可以看做軟鍵盤隱藏了 if(visibleHeight - rootViewVisibleHeight > 200){ if(onKeyBoardDisplayListener != null){ onKeyBoardDisplayListener.keyBoardHide(visibleHeight - rootViewVisibleHeight); } rootViewVisibleHeight = visibleHeight; return; } } }); } public interface OnKeyBoardDisplayListener{ void keyBoardShow(int height); void keyBoardHide(int height); } public void setOnSoftKeyBoardChangeListener(OnKeyBoardDisplayListener onKeyBoardDisplayListener) { this.onKeyBoardDisplayListener = onKeyBoardDisplayListener; } }

3,activity新增對軟鍵盤的監聽

在activity的onCreate新增

KeyboardListener softKeyboardListener = new KeyboardListener(this);
        softKeyboardListener.setOnSoftKeyBoardChangeListener(new KeyboardListener.OnKeyBoardDisplayListener() {
            @Override
            public void keyBoardShow(final int height) {
                /**獲取當前點選的控制元件**/
                View view=getWindow().getDecorView().findFocus();/**獲取座標。location[0] 為 x,1為y**/
                int[] location = new  int[2] ;
                view.getLocationInWindow(location);
                ScrollView scrollView=getView(R.id.scroll_view);int scroll_y_distance=height-(ScreenUtil.getScreenHeightPx(PostActivity.this)-location[1]);
                scrollView.setPadding(0,0,0,scroll_y_distance);
                scrollView.scrollTo(0,scrollView.getScrollY()+scroll_y_distance);
            }

            @Override
            public void keyBoardHide(int height) {
                ScrollView scrollView=getView(R.id.scroll_view);
                scrollView.setPadding(0,0,0,0);
            }
        });

4,xml檔案

所有的內容通過java程式碼追加到 content_container 裡

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Match2">
    <ScrollView
        android:id="@+id/scroll_view"
        style="@style/Match2" android:scrollbars="none">
      <LinearLayout
          android:id="@+id/content_container"
          style="@style/content_container">
      </LinearLayout>
    </ScrollView>
</FrameLayout>

到這裡基本上就能正常顯示軟鍵盤,不會破壞頁面佈局,也不會擔心edittext被軟鍵盤遮擋