登录界面键盘弹出时布局向上移动

在开发过程中会出现键盘遮挡输入框的情况,可以通过监测视图的变化进行回调键盘是否弹起
既然是监测键盘引起视图的变化那就需要设置以下条件才可以进行监听

要设置getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

或者在AndroidManifest.xmlactivity标签设置adjustPan

<activity
    android:name=".Activity"
    android:windowSoftInputMode="stateAlwaysHidden|adjustPan"/>

具体可以参考Android软键盘的全面解析,让你不再怕控件被遮盖

首先要定义一个检测键盘变化的类

/**
 * 检测软键盘是否隐藏
 * 要设置getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
 * https://blog.csdn.net/l540675759/article/details/74528641
 */
public class SoftKeyBoardListener {
    private View rootView;//activity的根视图
    int rootViewVisibleHeight;//纪录根视图的显示高度
    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
    private final ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;

    private SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();
        //获取当前根视图在屏幕上显示的大小
        //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
        //根视图显示高度变小超过200,可以看作软键盘显示了
        //根视图显示高度变大超过200,可以看作软键盘隐藏了
        mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取当前根视图在屏幕上显示的大小
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);
                int visibleHeight = r.height();
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }
                //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                //根视图显示高度变小超过200,可以看作软键盘显示了
                if (rootViewVisibleHeight - visibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度变大超过200,可以看作软键盘隐藏了
                if (visibleHeight - rootViewVisibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                }
            }
        };
        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
    }

    /**
     * 移除全局监听,否则会内存泄漏
     */
    public void removeGlobalLayoutListener() {
        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        if (rootView != null && mOnGlobalLayoutListener != null)
            rootView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

    public static SoftKeyBoardListener setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
        return softKeyBoardListener;
    }

}

这个是键盘弹出和消失检测的工具类,当检测到键盘弹出的时候给登录界面的布局加一个向上移动的动画就可以了.

登录界面的布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/login_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/loading_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="70dp"
        android:src="@drawable/logo" />

    <LinearLayout
        android:id="@+id/ll_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/login_edit"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:background="@null"
            android:hint="请输入账号"
            android:padding="12dp"
            android:textColor="#000"
            android:textColorHint="#b5b5b5"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/cb_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="记住账号"
            android:textColor="#32aaff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/login_edit"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:background="@null"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:padding="12dp"
            android:textColor="#000"
            android:textColorHint="#b5b5b5"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/cb_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="记住密码"
            android:textColor="#32aaff" />
    </LinearLayout>

    <Button
        android:id="@+id/login"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/selector_button"
        android:padding="12dp"
        android:text="登陆"
        android:textColor="#ffffff"
        android:textSize="14sp" />
</LinearLayout>

很简单的一个登录界面,获取到跟布局的对象之后进行一个属性动画设置

3.在activity中调用

private SoftKeyBoardListener softKeyBoardListener;

@Override
protected void onCreate() {
    super.onCreate();
    //绑定监听
    softKeyBoardListener = SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
        @Override
        public void keyBoardShow(int height) {
            ObjectAnimator valueAnimator = ObjectAnimator.ofFloat(mLoginRoot, "translationY", -100);
            valueAnimator.setDuration(300);
            valueAnimator.start();
        }

        @Override
        public void keyBoardHide(int height) {
            ObjectAnimator valueAnimator = ObjectAnimator.ofFloat(mLoginRoot, "translationY", 0);
            valueAnimator.setDuration(300);
            valueAnimator.start();
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();
    //解绑监听
    softKeyBoardListener.removeGlobalLayoutListener();
}

这样就完成了本次的任务, 当键盘弹出的时候就叫登录界面布局向上移动一些距离,当键盘隐藏的时候就恢复到原来的位置.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,196评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,109评论 4 62
  • 【罗马书7:18】我知道、在我里头、就是我肉体之中、没有良善.因为立志为善由得我、只是行出来由不得我。 很多基督徒...
    高桥先生阅读 747评论 0 0
  • 虽然第一天参加线下课程,但对于叶老师和易效能并不陌生,在过去的一年里,几乎每一天叶老师的声音都在陪伴着我,截...
    安安_02a8阅读 322评论 0 3
  • 校缘师缘同学缘(上联) 亲缘情缘事业缘(下联) 缘圆流长(横批)
    何启沪阅读 210评论 0 0