您的位置:首页 > 移动开发 > Android开发

Android之解决输入法软键盘弹出挤压屏幕或覆盖控件问题

2015-12-05 09:24 721 查看
最近在最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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: