Android软键盘遮挡的解决方案

0问题概述

在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图:

输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示:

1简单解决

1.1方法一

在你的activity中的oncreate中setContentView之前写上这个代码:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

1.2方法二

在项目的AndroidManifest.xml文件中界面对应的<activity>里加入 这样会让屏幕整体上移。如果加上的 是 android:windowSoftInputMode="adjustPan"这样键盘就会覆盖屏幕。那“android:windowSoftInputMode”是什么呢?activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性。这个属性能影响两件事情:
(1)当有焦点产生时,软键盘是隐藏还是显示
(2)是否减少活动主窗口大小以便腾出空间放软键盘
它的设置必须是下面列表中的一个值,或一个”state…”值加一个”adjust…”值的组合。在任一组设置多个值——多个”state…”values,例如&mdash有未定义的结果。各个值之间用|分开。
android:windowSoftInputMode="stateVisible|adjustPan"
在这设置的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆盖在主题中设置的值。Activity属性“android:windowSoftInputMode”有不同的值,它们的区别如下所示:
(1)stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
(2)stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
(3)stateHidden:用户选择activity时,软键盘总是被隐藏
(4)stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
(5)stateVisible:软键盘通常是可见的
(6)stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
(7)adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
(8)adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
(9)adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

1.3方法三

把顶级的layout替换成ScrollView,或者说在顶级的Layout上面再加一层ScrollView。这样就会把软键盘和输入框一起滚动了,软键盘会一直处于底部。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--解决方法三-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/login_bg" />

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="10dip"
            android:hint="@string/name" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:hint="@string/password" />
    </LinearLayout>

</ScrollView>

前面三种方法虽然简单,但是并不能达到预期的效果,方法四将使用代码可控的方法解决这个问题。

1.4方法四

1.4.1布局文件

以下为布局文件:keyboard_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.dyx.skd.view.SoftKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/layout_img"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:background="#ff0000"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:src="@mipmap/login_bg" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="3"
        android:orientation="vertical">

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center_vertical"
            android:ems="10"
            android:hint="@string/name">

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center_vertical"
            android:ems="10"
            android:hint="@string/password">

            <requestFocus />
        </EditText>
    </LinearLayout>
</com.dyx.skd.view.SoftKeyboardView>

1.4.2自定义LinearLayout

下面为自定义LinearLayout:SoftKeyboardView.java
package com.dyx.skd.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.LinearLayout;


/**
 * Created by Richard.Da on 2015/7/16.
 */
public class SoftKeyboardView extends LinearLayout {
    public static final int KEYBOARD_HIDE = 0;
    public static final int KEYBOARD_SHOW = 1;
    public static final int KEYBOARD_MIN_HEIGHT = 60;

    private Handler mHandler = new Handler();

    public SoftKeyboardView(Context context) {
        super(context);
    }

    public SoftKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, final int h, int oldw, final int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (oldh - h > KEYBOARD_MIN_HEIGHT) {
                    mKeyBoardStateChangeListener.stateChanged(KEYBOARD_SHOW);
                } else {
                    if (mKeyBoardStateChangeListener != null) {
                        mKeyBoardStateChangeListener.stateChanged(KEYBOARD_HIDE);
                    }
                }
            }
        });
    }

    private MyKeyBoardStateChangeListener mKeyBoardStateChangeListener;

    public void setMyKeyBoardStateChangeListener(MyKeyBoardStateChangeListener keyBoardStateChangeListener) {
        this.mKeyBoardStateChangeListener = keyBoardStateChangeListener;
    }

    //监听软键盘变化的接口
    public interface MyKeyBoardStateChangeListener {
        public void stateChanged(int stateCode);
    }
}

1.4.3主界面Activity

下面为界面Activity:MainActivity.java
package com.dyx.skd;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.dyx.skd.view.SoftKeyboardView;


public class MainActivity extends Activity implements SoftKeyboardView.MyKeyBoardStateChangeListener {
    private SoftKeyboardView rootLl;
    private LinearLayout imgLl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 解决方法一
         */
        // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.keyboard_layout);
        rootLl = (SoftKeyboardView) findViewById(R.id.layout_root);
        imgLl = (LinearLayout) findViewById(R.id.layout_img);
        rootLl.setMyKeyBoardStateChangeListener(this);
    }


    @Override
    public void stateChanged(int stateCode) {
        switch (stateCode) {
            case SoftKeyboardView.KEYBOARD_HIDE:
                imgLl.setVisibility(View.VISIBLE);
                break;
            case SoftKeyboardView.KEYBOARD_SHOW:
                imgLl.setVisibility(View.GONE);
                break;
        }
    }
}

1.4.4实现效果

键盘弹出:

键盘关闭:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值