Android之解決輸入法軟鍵盤彈出擠壓螢幕或覆蓋控制元件問題
阿新 • • 發佈:2019-02-07
最近在最Android開發過程中,出現一個問題:一個介面上有文字輸入框,當進行輸入時,輸入法軟鍵盤會彈出擠壓螢幕介面或者覆蓋控制元件。
解決辦法是:使用Window的setSoftInputMode()方法,明確設定軟鍵盤的輸入法模式:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
在Android官方文件中如下定義setSoftInputMode(int mode)方法:
/** * Specify an explicit soft input mode to use for the window, as per * {@link WindowManager.LayoutParams#softInputMode * WindowManager.LayoutParams.softInputMode}. Providing anything besides * "unspecified" here will override the input mode the window would * normally retrieve from its theme. */ public void setSoftInputMode(int mode) { final WindowManager.LayoutParams attrs = getAttributes(); if (mode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { attrs.softInputMode = mode; mHasSoftInputMode = true; } else { mHasSoftInputMode = false; } dispatchWindowAttributesChanged(attrs); }
其他常用的軟鍵盤輸入法模式還包括:
//軟鍵盤的狀態並沒有指定,系統將選擇一個合適的狀態或依賴於主題的設定
public static
final int SOFT_INPUT_STATE_UNSPECIFIED =
0;
//當這個activity出現時,軟鍵盤將一直保持在上一個activity裡的狀態,無論是隱藏還是顯示
public static
final int SOFT_INPUT_STATE_UNCHANGED =
1;
//使用者選擇activity時,軟鍵盤總是被隱藏
public static
final int SOFT_INPUT_STATE_HIDDEN =
2;
//當該Activity主視窗獲取焦點時,軟鍵盤也總是被隱藏的
public static final int SOFT_INPUT_STATE_ALWAYS_HIDDEN = 3;
//軟鍵盤通常是可見的
public static final int SOFT_INPUT_STATE_VISIBLE = 4;
//使用者選擇activity時,軟鍵盤總是顯示的狀態
public static final int SOFT_INPUT_STATE_ALWAYS_VISIBLE = 5;
//預設設定,通常由系統自行決定是隱藏還是顯示
public static final int SOFT_INPUT_ADJUST_UNSPECIFIED = 0x00;
//該Activity總是調整螢幕的大小以便留出軟鍵盤的空間
public static final int SOFT_INPUT_ADJUST_RESIZE = 0x10;
//當前視窗的內容將自動移動以便當前焦點從不被鍵盤覆蓋和使用者能總是看到輸入內容的部分
public static final int SOFT_INPUT_ADJUST_PAN = 0x20;
其中mode的值在WindowManager中定義如下:
/**
* Mask for {@link #softInputMode} of the bits that determine the
* desired visibility state of the soft input area for this window.
*/
public static final int SOFT_INPUT_MASK_STATE = 0x0f;
/**
* Visibility state for {@link #softInputMode}: no state has been specified.
*/
public static final int SOFT_INPUT_STATE_UNSPECIFIED = 0;
/**
* Visibility state for {@link #softInputMode}: please don't change the state of
* the soft input area.
*/
public static final int SOFT_INPUT_STATE_UNCHANGED = 1;
/**
* Visibility state for {@link #softInputMode}: please hide any soft input
* area when normally appropriate (when the user is navigating
* forward to your window).
*/
public static final int SOFT_INPUT_STATE_HIDDEN = 2;
/**
* Visibility state for {@link #softInputMode}: please always hide any
* soft input area when this window receives focus.
*/
public static final int SOFT_INPUT_STATE_ALWAYS_HIDDEN = 3;
/**
* Visibility state for {@link #softInputMode}: please show the soft
* input area when normally appropriate (when the user is navigating
* forward to your window).
*/
public static final int SOFT_INPUT_STATE_VISIBLE = 4;
/**
* Visibility state for {@link #softInputMode}: please always make the
* soft input area visible when this window receives input focus.
*/
public static final int SOFT_INPUT_STATE_ALWAYS_VISIBLE = 5;
/**
* Mask for {@link #softInputMode} of the bits that determine the
* way that the window should be adjusted to accommodate the soft
* input window.
*/
public static final int SOFT_INPUT_MASK_ADJUST = 0xf0;
/** Adjustment option for {@link #softInputMode}: nothing specified.
* The system will try to pick one or
* the other depending on the contents of the window.
*/
public static final int SOFT_INPUT_ADJUST_UNSPECIFIED = 0x00;
/** Adjustment option for {@link #softInputMode}: set to allow the
* window to be resized when an input
* method is shown, so that its contents are not covered by the input
* method. This can <em>not</em> be combined with
* {@link #SOFT_INPUT_ADJUST_PAN}; if
* neither of these are set, then the system will try to pick one or
* the other depending on the contents of the window. If the window's
* layout parameter flags include {@link #FLAG_FULLSCREEN}, this
* value for {@link #softInputMode} will be ignored; the window will
* not resize, but will stay fullscreen.
*/
public static final int SOFT_INPUT_ADJUST_RESIZE = 0x10;
/** Adjustment option for {@link #softInputMode}: set to have a window
* pan when an input method is
* shown, so it doesn't need to deal with resizing but just panned
* by the framework to ensure the current input focus is visible. This
* can <em>not</em> be combined with {@link #SOFT_INPUT_ADJUST_RESIZE}; if
* neither of these are set, then the system will try to pick one or
* the other depending on the contents of the window.
*/
public static final int SOFT_INPUT_ADJUST_PAN = 0x20;
/** Adjustment option for {@link #softInputMode}: set to have a window
* not adjust for a shown input method. The window will not be resized,
* and it will not be panned to make its focus visible.
*/
public static final int SOFT_INPUT_ADJUST_NOTHING = 0x30;
/**
* Bit for {@link #softInputMode}: set when the user has navigated
* forward to the window. This is normally set automatically for
* you by the system, though you may want to set it in certain cases
* when you are displaying a window yourself. This flag will always
* be cleared automatically after the window is displayed.
*/
public static final int SOFT_INPUT_IS_FORWARD_NAVIGATION = 0x100;
/**
* Desired operating mode for any soft input area. May be any combination
* of:
*
* <ul>
* <li> One of the visibility states
* {@link #SOFT_INPUT_STATE_UNSPECIFIED}, {@link #SOFT_INPUT_STATE_UNCHANGED},
* {@link #SOFT_INPUT_STATE_HIDDEN}, {@link #SOFT_INPUT_STATE_ALWAYS_VISIBLE}, or
* {@link #SOFT_INPUT_STATE_VISIBLE}.
* <li> One of the adjustment options
* {@link #SOFT_INPUT_ADJUST_UNSPECIFIED},
* {@link #SOFT_INPUT_ADJUST_RESIZE}, or
* {@link #SOFT_INPUT_ADJUST_PAN}.
* </ul>
*
*
* <p>This flag can be controlled in your theme through the
* {@link android.R.attr#windowSoftInputMode} attribute.</p>
*/
public int softInputMode;
轉載自:http://blog.csdn.net/wei_zhi/article/details/50183605