项目需求

(1)实现四个页面:首页、登录页、注册页、登陆成功页。

排版和跳转关系如图:

(2)注册页实现注册功能;登录页实现使用注册过的账号密码登录功能,登录成功后转到登录成功页;登录成功页有注销功能。

(3)具体功能:用户名必须为纯数字;注册页密码有二次验证功能;标题栏有返回键;成功登陆界面的文本根据用户名显示。

工程框架

主要实现代码

均在onActivityCreated()方法实现,布局文件代码省略。

没有的事

MainActivity

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
package com.example.myapplication;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController controller = Navigation.findNavController(this,R.id.fragment);
NavigationUI.setupActionBarWithNavController(this,controller);
}

@Override
public boolean onSupportNavigateUp(){
NavController controller = Navigation.findNavController(this,R.id.fragment);
return controller.navigateUp();
}
}

user(首页)部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

login_user = getView().findViewById(R.id.login_user);
login_user.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_user_to_login);
}
});

register_user = getView().findViewById(R.id.register_user);
register_user.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_user_to_register);
}
});
}

register(注册)部分

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
//对注册按钮监听
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = name_register.getText().toString().trim();
String password = password_register.getText().toString().trim();
String repass = pass_register.getText().toString().trim();
//连接数据库
databaseHelper = new MyDatebaseHelper(getActivity(), DATABASE_NAME, null, DATABASE_VERSION);
db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[]{"name"}, "name=?", new String[]{name}, null, null, null);
//判断账号是否全为数字
if (name.matches("[0-9]*")) {
//判断数据库中是否已有此账号
if (cursor != null && cursor.getCount() >= 1) {
Toast.makeText(getActivity(), "此用户已存在", Toast.LENGTH_LONG).show();
//刷新
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_register_self);
} else {
//判断输入框长度
if (!name.isEmpty() && !password.isEmpty() && !repass.isEmpty() && name.length() < 11 && password.length() < 11) {
if (password.equals(repass)) {
//上传
db.execSQL("insert into username (name,password) values(?,?)", new String[]{name, repass});
Toast.makeText(getActivity(), "注册成功,请登录", Toast.LENGTH_LONG).show();
//注册成功去到login界面
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_register_to_login);
} else {
Toast.makeText(getActivity(), "两次密码不一致", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "用户名和密码长度为1-10", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(getActivity(), "账号仅由数字组成", Toast.LENGTH_SHORT).show();
}
}
});
}

login(登录)部分

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
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = name_login.getText().toString().trim();
String pass = pass_login.getText().toString().trim();
if (!name.isEmpty() && !pass.isEmpty()) {
databaseHelper = new MyDatebaseHelper(getActivity(), DATABASE_NAME, null, DATABASE_VERSION);
db = databaseHelper.getReadableDatabase();
try {
Cursor cursor = db.query(TABLE_NAME, new String[]{"name", "password"}, "name=?", new String[]{name}, null, null, "password");
if (cursor != null && cursor.getCount() >= 1) {
while (cursor.moveToNext()) {
String password = cursor.getString(cursor.getColumnIndex("password"));
if (pass.equals(password)) {
//弹出登录成功框
new AlertDialog.Builder(getActivity()).setTitle("密码正确")
.setMessage("成功登录").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle args = new Bundle();
args.putString("username",name);
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_login_to_login_successful,args);

}
}).show();
break;
} else {
Toast.makeText(getActivity(), "密码错误", Toast.LENGTH_SHORT).show();
}
}
}else{
Toast.makeText(getActivity(), "该用户不存在", Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
e.printStackTrace();
}
}else {
Toast.makeText(getActivity(), "请填写完整", Toast.LENGTH_SHORT).show();
}
}
});

login_successful(登录成功页)部分

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
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
name_su = getView().findViewById(R.id.name_successful);

//实现功能
//传递login的name至此
Intent intent = getActivity().getIntent();
String name = intent.getStringExtra("name");
name_su.setText(name);
exitBtn = getView().findViewById(R.id.exitBtn);
//接收name
Bundle args = getArguments();
String user = args.getString("username");
//textView显示内容
name_su.setText("欢迎您:"+user);
exitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//注销,退出至首页
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_login_successful_to_user);
}
});
}

数据库类

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
package com.example.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

class MyDatebaseHelper extends SQLiteOpenHelper {
//数据库名称
private static final String DATABASE_NAME="sql.db";
//数据库版本号
private static final int DATABASE_VERSION=1;
//数据库SQL语句 添加一个表
public MyDatebaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table username( name varchar(30) primary key,password varchar(30))");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}

标题栏自定义

左上方返回键

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController controller = Navigation.findNavController(this,R.id.fragment);
NavigationUI.setupActionBarWithNavController(this,controller);
}

@Override
public boolean onSupportNavigateUp(){
NavController controller = Navigation.findNavController(this,R.id.fragment);
return controller.navigateUp();
}

属性

(1)设置标题栏的文字:在nav_graph_xml中,选中单个fragment,在Attributes中设置它的label即可。

(2)设置颜色:在themes.xml中找到<item name="colorPrimary">@color/purple_500</item>,在color.xml找到对应的<color name="purple_500">#FF00BFFF</color>,修改其中的颜色值即可。

总结

(1)可以增加登录输入验证码的功能。

(2)UI设计可以更加丰富。