1. 程式人生 > >Android面試題-鍵盤彈起擋住輸入框

Android面試題-鍵盤彈起擋住輸入框

原始碼分析相關面試題

Activity相關面試題

Service相關面試題

與XMPP相關面試題

與效能優化相關面試題

與登入相關面試題

與開發相關面試題

與人事相關面試題


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width
="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:background="#ff00ff"></LinearLayout> <EditText android:layout_width="match_parent" android:layout_height
="wrap_content"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定"/> </LinearLayout>

解決方法一:

最外層套ScrollView,控制元件多的時候可以自由滾動。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#ff00ff"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="確定"/>
    </LinearLayout>
</ScrollView>

效果如下圖:

套了ScrollView之後,雖然可以滾動了,馬馬虎虎也算解決了問題。

解決方法二:優雅解決

<?xml version="1.0" encoding="utf-8"?>
<com.maweiqi.recyclerview.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/ll_parent"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="#0000ff"/>

            <EditText
                android:id="@+id/account"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="確定"/>

        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>
</com.maweiqi.recyclerview.KeyboardLayout>

在activity當中呼叫setScroll();

//鍵盤不遮擋按鈕
    private void setScroll() {
        KeyboardUtil.assistActivity(this, R.id.scroll_view);       //這個是別人給我的工具類,只用這個會有

        parent.setOnTouchListener(new View.OnTouchListener() {                 //parent為Editext外面那層佈局
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                parent.setFocusable(true);
                parent.setFocusableInTouchMode(true);
                parent.requestFocus();
                InputMethodManager imm = (InputMethodManager) MainActivity.this
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(account.getWindowToken(), 0);  //隱藏鍵盤,account為Editext,隨便一個就好
                return false;
            }
        });

        scroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {         //scroll為parent外面那層佈局()最好用NestedScrollView,ScrollView會有版本問題
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                v.scrollTo(0,450);     //這個是滑動距離,隨便大一點就好
            }
        });
    }

鍵盤工具類:

public class KeyboardUtil {
    private static final String TAG = "KeyboardUtil";


    public static void assistActivity(Activity activity, int viewId) {
        new KeyboardUtil(activity, viewId);
    }

    private View mChildOfContent;
    private NestedScrollView mScrollView;
    private RelativeLayout.LayoutParams relativeLayoutParams;

    private KeyboardUtil(Activity activity, int viewId) {
        FrameLayout content = (FrameLayout) activity
                .findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mScrollView = (NestedScrollView) content.findViewById(viewId);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        possiblyResizeChildOfContent();
                    }
                });
    }

    private void possiblyResizeChildOfContent() {
        int contentHeight = mChildOfContent.getRootView().getHeight();
        int curDisplayHeight = computeUsableHeight();
        if (contentHeight - curDisplayHeight > contentHeight / 4) {
            Log.e(TAG, "possiblyResizeChildOfContent: 1" );
            mScrollView.scrollTo(0,600);
//                mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
        } else {
            Log.e(TAG, "possiblyResizeChildOfContent: 2" );
        }
    }

    /**
     * 獲取螢幕可顯示區域高度
     *
     * @return
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return r.height();
    }
}

自定義佈局:

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);
            }
        }
    }

}
  • 歡迎關注微信公眾號,長期推薦技術文章和技術視訊

  • 微信公眾號名稱:Android乾貨程式設計師