Android登录模块

 登录首页布局使用LinearLayout嵌套

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    >
    <LinearLayout
        android:id="@+id/layout_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="@mipmap/login_top"
        android:gravity="center"
        android:orientation="vertical"
        >
        <ImageView
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:src="@mipmap/logo"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textSize="20sp"
            android:textColor="@color/white"
            android:text="检测管理系统"
            />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="35dp"
            >
            <RelativeLayout
                android:id="@+id/relayone"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginLeft="35dp"
                android:layout_marginRight="35dp"
                android:gravity="center_vertical"
                android:orientation="horizontal" >
                <EditText
                    android:id="@+id/et_username"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:background="@color/touming"
                    android:hint="请输入账号"
                    android:imeOptions="actionNext"
                    android:inputType="text"
                    android:maxLength="32"
                    android:lines="1"
                    android:singleLine="true"
                    android:paddingRight="30dp"
                    android:layout_centerVertical="true"
                    android:textColor="@color/black"
                    android:textColorHint="#CACACA" />
                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginRight="5dp"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/yonghu" />
            </RelativeLayout>
            <View
                android:id="@+id/relayjg"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/gray"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_below="@id/relayone"
                />
            <RelativeLayout
                android:id="@+id/relaytwo"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="35dp"
                android:layout_below="@id/relayjg"
                android:layout_marginRight="35dp"
                android:gravity="center_vertical"
                android:orientation="horizontal" >
                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:background="@color/touming"
                    android:hint="请输入密码"
                    android:inputType="textPassword"
                    android:maxLength="32"
                    android:lines="1"
                    android:singleLine="true"
                    android:paddingRight="30dp"
                    android:layout_centerVertical="true"
                    android:textColor="@color/black"
                    android:textColorHint="#CACACA" />
                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginRight="5dp"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/mima" />
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/gray"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                />
            <CheckBox
                android:id="@+id/check_btn"
                android:layout_marginLeft="35dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="记住账号密码"
                android:checked="false"
                android:visibility="gone"
                />
        </LinearLayout>
        <Button
            android:id="@+id/loginbtn"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@drawable/login_top"
            android:layout_marginRight="40dp"
            android:layout_marginLeft="40dp"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:text="登录"
            />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical"
        >
        <LinearLayout
            android:id="@+id/layout_reset"
            android:layout_marginLeft="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:gravity="center"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:text="重置服务地址"
                android:textColor="#1587CA"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>
    </LinearLayout>
</LinearLayout>

页面代码

初始化控件

rootview = findViewById(R.id.rootview);
layout_top = findViewById(R.id.layout_top);
rootview.scrollTo(0,0);//解决部分opp手机初始化就滚动问题
keepLoginBtnNotOver(rootview, layout_top);
//触摸外部,键盘消失
rootview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Utils.closeKeyboard(LoginActivity.this);
        return false;
    }
});

其中实现方法

 /**
     * 保持登录按钮始终不会被覆盖
     *
     * @param root
     * @param subView
     */
    private void keepLoginBtnNotOver(final View root, final View subView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                // 获取root在窗体的可视区域
                root.getWindowVisibleDisplayFrame(rect);
                // 获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                // 若不可视区域高度大于200,则键盘显示,其实相当于键盘的高度
                if (rootInvisibleHeight > 200) {
                    // 显示键盘时
                    LogUtil.v("rootInvisibleHeight"+rootInvisibleHeight);
                    LogUtil.v("root"+root.getHeight());
                    LogUtil.v("subView"+subView.getHeight());
                    int srollHeight =rootInvisibleHeight - (root.getHeight() - subView.getHeight()) - Utils.getNavigationBarHeight(root.getContext());
                    LogUtil.v(""+srollHeight);
                    if (Math.abs(srollHeight) > 0) {//当键盘高度覆盖按钮时
//                         root.scrollTo(0,Math.abs(srollHeight>700?450:srollHeight));
                        root.scrollTo(0,Math.abs(ScreenUtil.dip2px(140)));//测试中小米5x和oppe手机部分机型正常
                    }
                } else {
                    // 隐藏键盘时
                    root.scrollTo(0, 0);
                }
            }
        });
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值