当前位置:网站首页>andorid实例-简单登录布局

andorid实例-简单登录布局

2020-11-09 17:46:00 ZHAO_JH

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#36A4D9"
    android:fitsSystemWindows="false"
    android:gravity="center|top"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--    通知栏留空变透明需要留空-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:textSize="30sp">
    </TextView>
    <!--标题-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="简易登录页面"
        android:textColor="#fff"
        android:textSize="30sp">
    </TextView>
    <!--    用户名和密码信息-->
    <RelativeLayout
        android:layout_width="356dp"
        android:layout_height="300dp"
        android:background="#fff">
        <!--    用户名信息-->
        <TextView
            android:id="@+id/user_text"
            android:layout_width="96dp"
            android:layout_height="50dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="50dp"
            android:background="#36A4D9"
            android:gravity="center"
            android:text="用户名:"
            android:textColor="#fff"
            android:textSize="20sp">
        </TextView>

        <EditText
            android:id="@+id/user_info"
            android:layout_width="203dp"
            android:layout_height="50dp"
            android:layout_marginTop="50dp"
            android:layout_toRightOf="@id/user_text"
            android:hint="请输入用户名">
        </EditText>
        <!--    密码信息-->
        <TextView
            android:id="@+id/pwd_text"
            android:layout_width="96dp"
            android:layout_height="50dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="110dp"
            android:background="#36A4D9"
            android:gravity="center"
            android:text="密    码:"
            android:textColor="#fff"
            android:textSize="20sp">
        </TextView>

        <EditText
            android:id="@+id/pwd_info"
            android:layout_width="203dp"
            android:layout_height="50dp"
            android:layout_marginTop="110dp"
            android:layout_toRightOf="@id/user_text"
            android:hint="请输入密码">
        </EditText>
        <!--        记住密码-->
        <CheckBox
            android:id="@+id/remember_info"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="170dp"
            android:text="记住用户名密码"
            android:textColor="#000"
            android:textSize="16sp">
        </CheckBox>
        <!--        登录按钮-->
        <Button
            android:id="@+id/login_button"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="230dp"
            android:background="#36A4D9"
            android:text="登    录"
            android:textColor="#fff"
            android:textSize="26sp">
        </Button>
    </RelativeLayout>
    <!--    版权声明信息-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:text="*此应用属个人测试,未经允许不得发布*"
        android:textColor="#fff"
        android:textSize="14sp">
    </TextView>
</LinearLayout>

MainActivity.java

package com.example.myapplication;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;



public class MainActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 保存Activity的状态  11
        super.onCreate(savedInstanceState);
        // 加载视图
        setContentView(R.layout.activity_main);

        // 安卓沉浸式设置透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        // 获取按钮对象
        Button btnshow = findViewById(R.id.login_button);
        //直接写个this绑定事件
        btnshow.setOnClickListener(this);
    }

    // 点击按钮方法
    @Override
    public void onClick(View v) {
        // 获取登录页面数据对象
        EditText user_obj = findViewById(R.id.user_info);
        EditText pwd_obj = findViewById(R.id.pwd_info);
        CheckBox remember_obj = findViewById(R.id.remember_info);

        Toast.makeText(getApplicationContext(), "您输入的用户名是:【"+user_obj.getText() + "】密码是:【"+pwd_obj.getText()+"】密码记住情况【"+remember_obj.isChecked()+"】" , Toast.LENGTH_SHORT).show();
    }
}

版权声明
本文为[ZHAO_JH]所创,转载请带上原文链接,感谢
https://my.oschina.net/zhaojunhui/blog/4710220