Android之解决输入法软键盘弹出挤压屏幕或覆盖控件问题
2015-12-05 09:24
721 查看
最近在最Android开发过程中,出现一个问题:一个界面上有文字输入框,当进行输入时,输入法软键盘会弹出挤压屏幕界面或者覆盖控件。
解决办法是:使用Window的setSoftInputMode()方法,明确设定软键盘的输入法模式:
在Android官方文档中如下定义setSoftInputMode(int mode)方法:
其他常用的软键盘输入法模式还包括:
其中mode的值在WindowManager中定义如下:
解决办法是:使用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;
相关文章推荐
- Android常见字体
- 安卓首屏广告开发
- viewpager+将activity转化成view 做主界面(可点击可滑动,超容易理解的demo)
- android(14)(ListView的列表展示,ArrayAdapter,SimpleAdapter)
- android有关paint属性设置
- android listview 错位
- Android控件之DatePickerDialog
- 关于java与Android(安卓)中为什么要在new一个对象的后面加一个this的原因
- 开发首屏广告(Android)简述
- Android 定时循环执行任务
- Android Studio NDK环境搭建与最简单例子编译执行
- Android四大组件简介
- 解决VS2015安装Android SDK 后文件不全及更新问题
- Android实现自动登录和记住密码
- android 开发中常见问题
- 深入理解Android Gradle
- Android 控件之DatePicker(日期和日历)
- Android Retrofit 框架上传多张图片解决方案
- android增加自定义公用类库customer-framework.jar
- Android自学之路,RecycleView的使用,添加与删除