巧妙地处理带头像的登录界面键盘遮挡问题

键盘弹出后总结起来主要是两点:

  1. 输入区域包括登录按钮整体上抬

  2. 原来的头像缩小

整体上抬

做到这个很简单,仅仅需要在AndroidManifest中对应的Activity的元素中加入windowSoftInputMode属性:

<activity
            android:name=".activity.LoginActivity"
            android:label="@string/login_button_text"
            android:launchMode="singleTask"
            android:windowSoftInputMode="stateHidden|stateUnchanged|adjustResize"/>

这显然不是我们想要的,虽然整体上抬了,但是登录按钮却依然被遮盖。输入完毕用户名密码后必须要先关闭输入法才能点击到登录按钮,等于多了一步操作。

那么到这里我们就想到了是否可以拿这个头像做文章?可以看到头像占据了很大的空间,那么是否可以在键盘抬起的时候我们调整头像的大小以及位置呢?答案是可以得,但是首先我们要做的是知道何时键盘打开或者关闭,其次再来改变这个头像。

监控键盘的状态

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/login_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_login_bg_start">

    ...

</RelativeLayout>

发现这个RelativeLayout的高度在键盘隐藏和抬起的时候是在变化的,那么它的onSideChange这个函数肯定是一直在被调用的。所以我们可以自己继承一个RelativeLayout,同时复写它的onSizeChange函数,通过高度的变化来判断键盘是否打开,同时暴露出一个接口给Activity实现,让Activity在该接口中根据键盘的状态来改变头像的布局。这个自定义的继承自RelativeLayout的控件如下:

package com.newbeeair.cleanser.ui.weigets;

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

import com.newbeeair.cleanser.utils.DebugLog;

/**
 * <pre>
 *     author : lzy
 *     e-mail : zanyang.lin@newbeeair.com
 *     time   : 2017/06/14
 *     desc   : 监听软键盘是否弹出
 * </pre>
 */

public class ResizeRelativeLayout extends RelativeLayout {

    public static final int HIDE = 0;
    public static final int SHOW = 1;

    private Handler mainHandler = new Handler();

    public ResizeRelativeLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ResizeRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onSizeChanged(int w, final int h, int oldw, final int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (oldh - h > 0) {
                    keyBordStateListener.onStateChange(SHOW);
                    DebugLog.e("SHOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                } else {
                    if (keyBordStateListener != null) {
                        keyBordStateListener.onStateChange(HIDE);

                        DebugLog.e("HIDE>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    }
                }
            }
        });
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private KeyBordStateListener keyBordStateListener;

    public void setKeyBordStateListener(KeyBordStateListener keyBordStateListener) {
        this.keyBordStateListener = keyBordStateListener;
    }

    public interface KeyBordStateListener {
        public void onStateChange(int state);
    }
}

头像的变化

有了键盘状态的监听后,头像变化的处理就变的随性了。为了简单起见,这里我们在根布局的右上角同样放置了一个ImageView,他和原来的头像ImageView显示同样的内容。当键盘弹起的时候显示右上角的头像而隐藏中间的大头像(注意一定要设置为Gone,这样子的话布局在onMeasure的时候才会把原来属于大头像的空间腾出来从而让下方的输入框和登录按钮向上抬起的更多);当键盘隐藏的时候则显示中间大头像而隐藏右上角的头像。

  rlLoginRoot.setKeyBordStateListener(new ResizeRelativeLayout.KeyBordStateListener() {
            @Override
            public void onStateChange(int state) {
                switch (state) {
                    case ResizeRelativeLayout.HIDE:
                        //TODO when keyboard is hide
//                        ivLoginLogo.setVisibility(View.VISIBLE);
//                        ivLoginLogoSmall.setVisibility(View.GONE);
                        ivLoginLogo.startAnimation(mBigAnimation);
                        DebugLog.e("HIDE>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                        break;
                    case ResizeRelativeLayout.SHOW:
                        //TODO when keyboard is show
                        ivLoginLogo.startAnimation(mLitteAnimation);
//                        ivLoginLogo.setVisibility(View.GONE);
//                        ivLoginLogoSmall.setVisibility(View.VISIBLE);
                        DebugLog.e("SHOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                        break;
                }
            }
        });

如果需要做的带感的话,可以对中间的大头像做如下的属性动画(一定要是属性动画,因为这样才能真正的“移动”而腾出空间)而不用显示另一个右上角的小头像:

  1. ImageView通过scaleX和scaleY来缩小size

  2. ImageView通过translateX和translateY来移动到右上角

<com.newbeeair.cleanser.ui.weigets.ResizeRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl_login_root"
    android:background="@drawable/bg_splash">

    <ImageView
        android:id="@+id/iv_login_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:background="@drawable/ic_logo_large" />

    <ImageView
        android:id="@+id/iv_login_logo_small"
        android:layout_width="40dp"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:visibility="gone"
        android:background="@drawable/ic_logo_large" />


    <!--输入用户账号-->
    <RelativeLayout
        android:id="@+id/rl_login_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/iv_login_logo"
        android:layout_marginLeft="42dp"
        android:layout_marginRight="42dp"
        android:layout_marginTop="120dp"
        android:background="@drawable/shape_white"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_login_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:src="@drawable/ic_user" />

        <EditText
            android:id="@+id/et_login_name"
            style="@style/editText_item_style"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="45dp"
            android:layout_toRightOf="@+id/iv_login_name"
            android:hint="账号"
            android:inputType="text"
            android:maxLength="30"
            android:textColor="@color/black_666666"
            android:textColorHint="@color/grey_c4c4c4"
            android:textSize="14sp" />

    </RelativeLayout>
    <!--输入密码-->
    <RelativeLayout
        android:id="@+id/rl_login_psw"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/rl_login_name"
        android:layout_marginLeft="42dp"
        android:layout_marginRight="42dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/shape_white"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_login_psw"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="8dp"
            android:src="@drawable/ic_psw" />

        <EditText
            android:id="@+id/et_login_psw"
            style="@style/editText_item_style"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="45dp"
            android:layout_toRightOf="@+id/iv_login_psw"
            android:hint="密码"
            android:inputType="textPassword"
            android:maxLength="16"
            android:password="true"
            android:textColor="@color/black_666666"
            android:textColorHint="@color/grey_d1d1d1"
            android:textSize="14sp" />

    </RelativeLayout>

    <Button
        android:id="@+id/btn_login_login"
        style="@style/ButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_below="@+id/rl_login_psw"
        android:layout_marginLeft="42dp"
        android:layout_marginRight="42dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/bg_border_rec"
        android:enabled="false"
        android:text="登录"
        android:textColor="@color/white_ffffff"
        android:textSize="14sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_login_login"
        android:layout_marginLeft="42dp"
        android:layout_marginRight="42dp"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/tv_login_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="忘记密码"
            android:textColor="@color/white_ffffff"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/tv_login_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="注册账号"
            android:textColor="@color/white_ffffff"
            android:textSize="10sp" />


    </RelativeLayout>


</com.newbeeair.cleanser.ui.weigets.ResizeRelativeLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容