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

最近在最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;
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android手机的输入时,可能有以下几个原因: 1. 输入未启用:首先要确保输入已经启用。可以在手机设置中的“语言和输入”或“键盘输入”中查看所有安装的输入,并选择需要使用的输入。如果没有检测到要使用的输入,可以尝试下载并安装相应的输入应用程序。 2. 输入未被选中:在应用程序中,点击文本输入框时,可能需要手动选择要使用的输入。通过长按文本输入框,会选项菜单,选择“输入”并选择需要使用的输入。 3. 输入冲突:如果安装了多个输入,可能会输入冲突的情况,导致输入。可以尝试禁用除需要使用的输入外的其他输入,然后重新启动手机。 4. 件冲突:某些应用程序可能与输入件存在冲突,导致无输入。可以尝试卸载最近安装的应用程序,或在应用管理器中找到相关应用程序并清除其缓存数据。 5. 系统问题:偶尔,系统中的某些错误可能导致输入。可以尝试重启手机,以解决可能存在的临时问题。如果问题依然存在,可以考虑进行硬件重置或寻求专业技术支持。 总而言之,可以通过检查输入的启用状态、选中输入解决输入冲突、处理件冲突、重启手机等方解决Android手机输入问题。如果问题仍然无解决,建议寻求专业技术支持。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值