登录界面

目标

主要完成对登录界面的设计,输入用户名和密码跳转至主界面
注:仅实现此功能,其他功能并未实现
登录界面如下

实现过程

布局文件

界面xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
tools:context=".ui.login.login">

<TextView
android:id="@+id/findPassword_text"
android:layout_width="142dp"
android:layout_height="45dp"
android:gravity="center"
android:text="找回密码"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintRight_toRightOf="@id/btn_login"
app:layout_constraintTop_toBottomOf="@id/btn_login" />

<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/register_selector"
android:text="登录"
android:textColor="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/Password" />

<ImageView
android:id="@+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="484dp"
app:layout_constraintTop_toBottomOf="@+id/login_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/default_icon" />

<EditText
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="416dp"
android:background="@drawable/login_user_name_bg"
android:drawableLeft="@drawable/user_name_icon"
android:ems="10"
android:hint="请输入用户名"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

<EditText
android:id="@+id/Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="52dp"
android:layout_marginBottom="264dp"
android:background="@drawable/login_psw_bg"
android:ems="10"
android:hint="请输入密码"
android:drawableLeft="@drawable/psw_icon"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/userName"
app:layout_constraintLeft_toLeftOf="@+id/userName" />

<ImageView
android:id="@+id/back_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/go_back_selector"
app:srcCompat="@drawable/iv_back" />

<TextView
android:id="@+id/login_title"
android:layout_width="230dp"
android:layout_height="76dp"
android:gravity="center"
android:text="登录"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteX="80dp"
tools:layout_editor_absoluteY="0dp" />

<TextView
android:id="@+id/register_text"
android:layout_width="142dp"
android:layout_height="45dp"
android:text="立即注册"
android:textColor="@color/white"
android:gravity="center"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/btn_login"
app:layout_constraintLeft_toLeftOf="@id/btn_login"
tools:layout_editor_absoluteX="51dp"
tools:layout_editor_absoluteY="492dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

按钮选择器register_selector.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/register_icon_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/register_icon_normal"/>
</selector>

返回图标go_back_selector.xml

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:drawable="@drawable/iv_back_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/iv_back"/>
</selector>

Java代码

需要用户名和密码同时输入test时,界面跳转至主界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example.mytest.ui.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mytest.MainActivity;
import com.example.mytest.R;

public class login extends AppCompatActivity implements View.OnClickListener{

Button btn_loginIn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);

btn_loginIn = (Button) findViewById(R.id.btn_login);
btn_loginIn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
String userName="",password="";
EditText editText1 =(EditText)findViewById(R.id.userName);
userName=editText1.getText().toString();
EditText editText2 =(EditText)findViewById(R.id.Password);
password=editText2.getText().toString();

if (v.getId() == btn_loginIn.getId()&&userName.equals("test")&&password.equals("test")){
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
startActivity(intent);
}
else {
Toast.makeText(this, "账号密码错误", Toast.LENGTH_SHORT).show();
}

}
}

项目地址

github项目地址