1. 程式人生 > 其它 >Android中解決鍵盤彈出時輸入框被遮擋問題

Android中解決鍵盤彈出時輸入框被遮擋問題

技術標籤:andrioid

1.鍵盤彈出時螢幕可以滑動
在佈局裡面新增

android:fitsSystemWindows="true"

2.鍵盤彈出時把所有內容頂上去
(1).首先一個工具類:

public class KeyboardLayout extends FrameLayout {

    private KeyboardLayoutListener mListener;
    private boolean mIsKeyboardActive = false; //輸入法是否啟用
    private int mKeyboardHeight = 0; // 輸入法高度

    public KeyboardLayout(Context context) {
        this(context, null, 0);
    }

    public KeyboardLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KeyboardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 監聽佈局變化
        getViewTreeObserver().addOnGlobalLayoutListener(new KeyboardOnGlobalChangeListener());
    }

    public void setKeyboardListener(KeyboardLayoutListener listener) {
        mListener = listener;
    }

    public KeyboardLayoutListener getKeyboardListener() {
        return mListener;
    }

    public boolean isKeyboardActive() {
        return mIsKeyboardActive;
    }

    /**
     * 獲取輸入法高度
     *
     * @return
     */
    public int getKeyboardHeight() {
        return mKeyboardHeight;
    }

    public interface KeyboardLayoutListener {
        /**
         * @param isActive       輸入法是否啟用
         * @param keyboardHeight 輸入法面板高度
         */
        void onKeyboardStateChanged(boolean isActive, int keyboardHeight);
    }

    private class KeyboardOnGlobalChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
        int mScreenHeight = 0;
        private int getScreenHeight() {
            if (mScreenHeight > 0) {
                return mScreenHeight;
            }
            mScreenHeight = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay().getHeight();
            return mScreenHeight;
        }

        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            // 獲取當前頁面視窗的顯示範圍
            ((Activity) getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int screenHeight = getScreenHeight();
            int keyboardHeight = screenHeight - rect.bottom; // 輸入法的高度
            boolean isActive = false;
            if (Math.abs(keyboardHeight) > screenHeight / 4) {
                isActive = true; // 超過螢幕五分之一則表示彈出了輸入法
                mKeyboardHeight = keyboardHeight;
            }
            mIsKeyboardActive = isActive;
            if (mListener != null) {
                mListener.onKeyboardStateChanged(isActive, keyboardHeight);
            }
        }
    }
}

(2).在佈局最外層引用這個控制元件,並設定id

<com.lmc.cityrabbit.utils.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".view.activity.ContractContentActivity">

(3).清單檔案對應Activity新增

<activity android:name=".view.activity.RentActivity"
            android:windowSoftInputMode="adjustResize"
            />

(4).Activity裡面

keyboard.setKeyboardListener((isActive, keyboardHeight) -> {
            if (isActive) {
                scrollToBottom();
            }
        });

private void scrollToBottom() {
        scroll.postDelayed(new Runnable() {
            @Override
            public void run() {
                scroll.smoothScrollTo(0, scroll.getBottom());
            }
        }, 100);
    }

3.最外層佈局使用ScrollView(還未嘗試此方法)