天天看點

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

源碼分析相關面試題

  • Volley源碼分析
  • 注解架構實作原理
  • okhttp3.0源碼分析
  • onSaveInstanceState源碼分析

Activity相關面試題

  • 儲存Activity的狀态
  • activity的啟動模式原理(一)
  • activity的啟動模式原理(二)
  • activity的啟動模式原理(三)
  • ActivityRecord TaskRecord和ProcessRecord之間的關系
  • service裡面startActivity抛異常?activity不會
  • 如何退出Activity?如何安全退出已調用多個Activity的Application?

Service相關面試題

  • IntentService源碼分析
  • Service是否在main thread中執行, service裡面是否能執行耗時的操作
  • service被kill之後怎麼讓它重新開機

與XMPP相關面試題

  • 與XMPP相關試題一
  • 與XMPP相關試題二

與性能優化相關面試題

  • 與性能優化相關面試題一
  • 與性能優化相關面試題二
  • 與性能優化相關面試題三
  • 與性能優化相關面試題四
  • 與性能優化相關面試題五
  • 與性能優化相關面試題六
  • 與IPC機制相關面試題

與登入相關面試題

  • oauth認證協定原理
  • token産生的意義
  • 微信掃一掃實作原理

與開發相關面試題

  • 疊代開發的時候如何向前相容新舊接口
  • 手把手教你如何解決as jar包沖突
  • context的原理分析
  • 解決ViewPager.setCurrentItem中間很多頁面切換方案
  • 建立虛拟機時報錯 Please file a bug against Android Studio
  • 字型适配
  • 鍵盤彈起擋住輸入框
  • 機型适配之痛,例如三星、小米、華為、魅族等

與人事相關面試題

  • 人事面試寶典
Android面試題-鍵盤彈起擋住輸入框
Android面試題-鍵盤彈起擋住輸入框
<?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>
           

效果如下圖:

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

套了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(), );  //隐藏鍵盤,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(,);     //這個是滑動距離,随便大一點就好
            }
        });
    }
           

鍵盤工具類:

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();
        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 / ) {
            Log.e(TAG, "possiblyResizeChildOfContent: 1" );
            mScrollView.scrollTo(,);
//                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 = ; // 輸入法高度

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

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

    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 = ;

        private int getScreenHeight() {
            if (mScreenHeight > ) {
                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 / ) {
                isActive = true; // 超過螢幕五分之一則表示彈出了輸入法
                mKeyboardHeight = keyboardHeight;
            }
            mIsKeyboardActive = isActive;
            if (mListener != null) {
                mListener.onKeyboardStateChanged(isActive, keyboardHeight);
            }
        }
    }

}
           
  • 歡迎關注微信公衆号,長期推薦技術文章和技術視訊
  • 微信公衆号名稱:Android幹貨程式員
Android面試題-鍵盤彈起擋住輸入框

繼續閱讀