功能描述
(1)实现简单的登陆、注册功能。
(2)在注册功能里添加了验证码功能(点击验证码图片会更换验证码)
(3)注册信息会存到SQLite数据库。
小说明:我完整的项目是要求实现“记住密码”和“自动登录”功能的,但是想实现简单的登陆、注册功能就没必要了(这不是个没有便不行的功能,就让代码简单点吧,以下代码不包含记住密码、自动登录功能)
效果图
数据库
代码
- LoginActivity
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
//用户名
EditText userNameTxt;
//密码
EditText passwordTxt;
//登录按钮
Button loginBtn;
//注册按钮
Button registerBtn;
private DBOpenHelper mDBOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
mDBOpenHelper = new DBOpenHelper(this);
}
private void initView(){
// 初始化控件
userNameTxt =(EditText)findViewById(R.id.userNameTxt);
passwordTxt = (EditText) findViewById(R.id.passwordTxt);
loginBtn = (Button)findViewById(R.id.loginBtn);
registerBtn = (Button) findViewById(R.id.registerBtn);
// 设置点击事件监听器
loginBtn.setOnClickListener(this);
registerBtn.setOnClickListener(this);
}
public void onClick(View view){
switch (view.getId()) {
// 跳转到注册界面
case R.id.registerBtn:
startActivity(new Intent(this, RegisterActivity.class));
finish();
break;
/**
* 登录验证:
*
* 从EditText的对象上获取文本编辑框输入的数据,并把左右两边的空格去掉
* String name = mEtLoginactivityUsername.getText().toString().trim();
* String password = mEtLoginactivityPassword.getText().toString().trim();
* 进行匹配验证,先判断一下用户名密码是否为空,
* if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password))
* 再进而for循环判断是否与数据库中的数据相匹配
* if (name.equals(user.getName()) && password.equals(user.getPassword()))
* 一旦匹配,立即将match = true;break;
* 否则 一直匹配到结束 match = false;
*
* 登录成功之后,进行页面跳转:
*
* Intent intent = new Intent(this, MainActivity.class);
* startActivity(intent);
* finish();//销毁此Activity
*/
case R.id.loginBtn:
String name = userNameTxt.getText().toString().trim();
String password = passwordTxt.getText().toString().trim();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
ArrayList<User> data = mDBOpenHelper.getAllData();
boolean match = false;
for (int i = 0; i < data.size(); i++) {
User user = data.get(i);
if (name.equals(user.getName()) && password.equals(user.getPassword())) {
match = true;
break;
} else {
match = false;
}
}
if (match) {
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();//销毁此Activity
} else {
Toast.makeText(this, "用户名或密码不正确,请重新输入", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
- RegisterActivity
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView mIvRegisteractivityShowcode;
private DBOpenHelper mDBOpenHelper;
private EditText mEtRegisteractivityPhonecodes;
private String realCode;
//用户名
EditText userNameTxt;
//密码
EditText passwordTxt;
//确认密码
EditText passwordTxtAgain;
//返回登录按钮
Button backBtn;
//注册按钮
Button registerBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initView();
mDBOpenHelper = new DBOpenHelper(this);
//将验证码用图片的形式显示出来
mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
realCode = Code.getInstance().getCode().toLowerCase();
}
private void initView() {
//初始化各个组件
userNameTxt =(EditText)findViewById(R.id.userNameTxt);
passwordTxt = (EditText) findViewById(R.id.passwordTxt);
backBtn = (Button)findViewById(R.id.backBtn);
registerBtn = (Button) findViewById(R.id.registerBtn);
passwordTxtAgain = findViewById(R.id.passwordTxtAgain);
mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode);//验证码图片
mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes);//验证码
backBtn.setOnClickListener(this);
registerBtn.setOnClickListener(this);
mIvRegisteractivityShowcode.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.backBtn: //返回登录页面
Intent intent1 = new Intent(this, LoginActivity.class);
startActivity(intent1);
finish();
break;
case R.id.iv_registeractivity_showCode: //改变随机验证码的生成
mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
realCode = Code.getInstance().getCode().toLowerCase();//转为小写,降低难度,否则还要区分大小写
break;
case R.id.registerBtn: //注册按钮
//获取用户输入的用户名、密码、验证码
String username = userNameTxt.getText().toString().trim();
String password = passwordTxtAgain.getText().toString().trim();
String userInputCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase();
//注册验证
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(userInputCode) ) {
//用TextUtils.equals()不用string的equals,TextUtils.equals()多了非空判断
if (TextUtils.equals(userInputCode,realCode)) {
//将用户名和密码加入到数据库中
mDBOpenHelper.add(username, password);
Intent intent2 = new Intent(this, MainActivity.class);
startActivity(intent2);
finish();
Toast.makeText(this, "验证通过,注册成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(this, "未完善信息,注册失败", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
布局代码
- activity_login.xml
<?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"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<!-- 标题 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="用户登录"
android:textColor="#000"
android:textSize="35sp" />
<!-- 用户名 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="95dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:layout_marginLeft="50dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<EditText
android:id="@+id/userNameTxt"
android:hint="请输入用户名"
android:textColorHint="@color/colorTiShi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"/>
</LinearLayout>
<!-- 密码 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:layout_marginLeft="50dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<EditText
android:id="@+id/passwordTxt"
android:hint="请输入密码"
android:textColorHint="@color/colorTiShi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginRight="50dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp">
<CheckBox
android:id="@+id/remenber_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorWhite"
android:textColor="@color/colorBlack"
android:text="记住密码" />
<CheckBox
android:id="@+id/login_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorWhite"
android:textColor="@color/colorBlack"
android:text="自动登录" />
</LinearLayout>
<!-- 登录按钮 -->
<!-- 成功或失败提示 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
>
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_weight="1"
android:background="@color/colorWhite"
android:textColor="@color/colorBlack"
android:textSize="20sp" />
<Button
android:id="@+id/registerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_weight="1"
android:textColor="@color/colorBlack"
android:background="@color/colorWhite"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
- activity_register.xml
<?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"
tools:context=".RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<!-- 标题 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="用户注册"
android:textColor="@color/colorBlack"
android:textSize="35sp" />
<!-- 用户名 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="95dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用 户 名:"
android:layout_marginLeft="50dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<EditText
android:id="@+id/userNameTxt"
android:hint="请输入用户名"
android:textColorHint="@color/colorTiShi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"/>
</LinearLayout>
<!-- 密码 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:layout_marginLeft="50dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<EditText
android:id="@+id/passwordTxt"
android:hint="请输入密码"
android:textColorHint="@color/colorTiShi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginRight="50dp"/>
</LinearLayout>
<!-- 确认密码 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"
android:layout_marginLeft="50dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<EditText
android:id="@+id/passwordTxtAgain"
android:hint="请再次输入密码吗"
android:textColorHint="@color/colorTiShi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginRight="50dp"/>
</LinearLayout>
<!-- 输入验证码 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_registeractivity_phoneCodes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="验证码:"
android:layout_marginLeft="50dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<EditText
android:id="@+id/et_registeractivity_phoneCodes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="50dp"
android:hint="请输入4位验证码" />
</LinearLayout>
<!--显示验证码-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_registeractivity_showCode"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_marginLeft="140dp"
android:clickable="true"
android:onClick="onClick"
android:layout_marginRight="50dp" />
</LinearLayout>
<!-- 注册、登录按钮 -->
<!-- 成功或失败提示 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
>
<Button
android:id="@+id/registerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认注册"
android:layout_weight="1"
android:textColor="@color/colorBlack"
android:background="@color/colorWhite"
android:textSize="20sp" />
<TextView
android:id="@+id/tipsTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="注册失败"
android:visibility="gone"
android:textSize="18sp"/>
<Button
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回登录"
android:layout_weight="1"
android:background="@color/colorWhite"
android:textColor="@color/colorBlack"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>