Android使用LitePal实现用户注册、登录功能 真菜鸟食用
参考郭神的《第一行代码》,随着安卓的版本更新做了一点改进。水平太低,还望见谅。这位大神整理的Litepal知识较简单
https://blog.csdn.net/xxdw1992/article/details/104937107?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162374194316780264028668%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=162374194316780264028668&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-104937107.pc_search_result_control_group&utm_term=litepal&spm=1018.2226.3001.4187
圆形图片见
https://blog.csdn.net/m0_46651408/article/details/117825793?spm=1001.2014.3001.5501
主页
虚假的微信登陆
注册页
登录页
显示页
1.1Litepal引入库
在gradle的依赖中引入
implementation 'org.litepal.guolindev:core:3.1.1'
1.2配置Litepal
1)main->新建->目录,创建assets目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0f3457cd18017732caaa95efa2f379aa.png)
一定要新建文件自己输入文件尾,不要直接创建.xml文件
1.3litepal.xml
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="User" ></dbname>
<version value="1" ></version>
<list>
<mapping class="com.example.jh_android.User"></mapping>
</list>
</litepal>
1.4修改AndroidManifest.xml文件
<application
android:name="org.litepal.LitePalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
.....
Litepal到此配置完成!
2JavaBean User.java
继承LitePalSupportimport org.litepal.crud.LitePalSupport;
public class User extends LitePalSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.1主页布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:id="@+id/show_text">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="JIA HUI"
android:textColor="@color/white"
android:textSize="70dp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_login"
android:layout_below="@id/show_text"
android:layout_marginTop="200dp"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_jump_login"
android:layout_width="300dp"
android:layout_height="50dp"
android:text="用户登录"
android:textSize="18dp"
android:background="@color/white"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_login"
android:layout_marginTop="10dp"
android:gravity="center">
<Button
android:id="@+id/btn_wechat"
android:layout_width="142dp"
android:layout_height="50dp"
android:layout_marginRight="6dp"
android:background="@drawable/register_button"
android:textSize="18dp"
android:text="微信登录"
android:textColor="@color/white"/>
<Button
android:id="@+id/btn_register"
android:layout_width="142dp"
android:layout_height="50dp"
android:layout_marginLeft="6dp"
android:background="@drawable/register_button"
android:textSize="18dp"
android:text="新用户注册"
android:textColor="@color/white"/>
</LinearLayout>
</RelativeLayout>
3.2注册页布局activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@drawable/background"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<com.example.jh_android.RoundPicture
android:id="@+id/iv"
android:layout_height="200dp"
android:layout_width="200dp"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:src="@drawable/head"
/>
<LinearLayout
android:id="@+id/ll_username"
android:layout_below="@id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_centerVertical="true"
android:background="#6B009688">
<TextView
android:id="@+id/tv_register_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:padding="10dp"
android:textSize="20dp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/et_register_username"
android:layout_width="match_parent"
android:maxLines="1"
android:hint="用户名需小于16位字符"
android:maxLength="16"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@null"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_password"
android:layout_below="@id/ll_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:background="#6B009688">
<TextView
android:id="@+id/tv_register_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:padding="10dp"
android:textSize="20dp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/et_register_password"
android:layout_width="255dp"
android:maxLength="6"
android:maxLines="1"
android:hint="密码需大于6个字符"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@null"/>
<ImageView android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginTop="14dp"
android:id="@+id/iv_register_password"/>
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_btm"
android:layout_below="@id/ll_password"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_register"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:text="注册"
android:textSize="18dp"
android:background="@color/white"
/>
</LinearLayout>
</RelativeLayout>
3.3登录页布局activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@drawable/background"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<com.example.jh_android.RoundPicture
android:id="@+id/iv"
android:layout_height="200dp"
android:layout_width="200dp"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:src="@drawable/head"
/>
<LinearLayout
android:id="@+id/ll_username"
android:layout_below="@id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_centerVertical="true"
android:background="#6B009688">
<TextView
android:id="@+id/tv_login_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:padding="10dp"
android:textSize="20dp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/et_login_username"
android:maxLines="1"
android:maxLength="16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@null"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_password"
android:layout_below="@id/ll_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:background="#6B009688">
<TextView
android:id="@+id/tv_login_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:padding="10dp"
android:textSize="20dp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/et_login_password"
android:maxLines="1"
android:maxLength="6"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@null"/>
<ImageView android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginTop="14dp"
android:id="@+id/display_password"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_btm"
android:layout_below="@id/ll_password"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_login"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="登录"
android:textSize="18dp"
android:background="@color/white"
/>
</LinearLayout>
</RelativeLayout>
3.4显示页布局activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShowActivity">
<com.example.jh_android.RoundPicture
android:id="@+id/iv"
android:layout_height="200dp"
android:layout_width="200dp"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:src="@drawable/head"
/>
<LinearLayout
android:id="@+id/ll_username"
android:layout_below="@id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_centerVertical="true"
android:background="#6B009688">
<TextView
android:id="@+id/show_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:textSize="20dp"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_password"
android:layout_below="@id/ll_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:background="#6B009688">
<TextView
android:id="@+id/show_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:textSize="20dp"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_below="@id/ll_password"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_delete"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="注销账号"
android:textSize="18dp"
android:background="@color/white"
/>
</LinearLayout>
</RelativeLayout>
3.5主页MainActivity.java
package com.example.jh_android;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.litepal.LitePal;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.hide();
}
setContentView(R.layout.activity_main);
Button btn_jump_login = (Button) findViewById(R.id.btn_jump_login);
Button btn_wechat = (Button) findViewById(R.id.btn_wechat);
Button btn_register = (Button) findViewById(R.id.btn_register);
btn_jump_login.setOnClickListener(this);
btn_wechat.setOnClickListener(this);
btn_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_jump_login:
Intent loginintent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(loginintent);
break;
case R.id.btn_wechat:
Toast.makeText(MainActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_register:
Intent registerintent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(registerintent);
break;
}
}
}
3.6注册页RegisterActivity.java
package com.example.jh_android;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;
import java.util.List;
public class RegisterActivity extends AppCompatActivity {
private EditText registerUsername;
private EditText registerPassword;
private Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
register = findViewById(R.id.btn_register);
registerUsername = findViewById(R.id.et_register_username);
registerPassword = findViewById(R.id.et_register_password);
register.setOnClickListener(v -> {
String username = registerUsername.getText().toString();
String password = registerPassword.getText().toString();
if (username.equals("") || password.equals("")) {
Toast.makeText(RegisterActivity.this, "账号或密码为空", Toast.LENGTH_SHORT).show();
return;
}
List<User> users = LitePal.findAll(User.class);
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
Toast.makeText(RegisterActivity.this, "账号不可以重复注册", Toast.LENGTH_SHORT).show();
return;
}
}
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.save();
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent showintent = new Intent(RegisterActivity.this, ShowActivity.class);
showintent.putExtra("username",username);
showintent.putExtra("password",password);
startActivity(showintent);
});
}
}
3.7登录页LoginActivity.java
package com.example.jh_android;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.litepal.LitePal;
import java.util.List;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText loginUsername;
private EditText loginPassword;
private Button login;
private ImageView displayPassword;
private boolean isHideFirst = false;
// private CheckBox rememberCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
login = findViewById(R.id.btn_login);
loginUsername = findViewById(R.id.et_login_username);
loginPassword = findViewById(R.id.et_login_password);
displayPassword = findViewById(R.id.display_password);
// rememberCheck = findViewById(R.id.remember_check);
login.setOnClickListener(this);
displayPassword.setOnClickListener(this);
displayPassword.setImageResource(R.drawable.open);
}
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.display_password:{
if (isHideFirst) {
displayPassword.setImageResource(R.drawable.open);
HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
loginPassword.setTransformationMethod(method1);
isHideFirst = false;
} else {
displayPassword.setImageResource(R.drawable.close);
TransformationMethod method = PasswordTransformationMethod.getInstance();
loginPassword.setTransformationMethod(method);
isHideFirst = true;
}
int index = loginPassword.getText().toString().length();
loginPassword.setSelection(index);
break;
}
case R.id.btn_login: {
String username = loginUsername.getText().toString();
String password = loginPassword.getText().toString();
if (username.equals("") || password.equals("")) {
Toast.makeText(LoginActivity.this, "账号或密码为空", Toast.LENGTH_SHORT).show();
return;
}
List<User> users = LitePal.findAll(User.class);
for (User user : users) {
if (!user.getUsername().equals(username)) {
Toast.makeText(LoginActivity.this, "账号未注册!", Toast.LENGTH_SHORT).show();
return;
}
if (user.getUsername().equals(username) && !user.getPassword().equals(password)) {
Toast.makeText(LoginActivity.this, "密码不正确!", Toast.LENGTH_SHORT).show();
return;
}
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
Intent showintent = new Intent(LoginActivity.this, ShowActivity.class);
showintent.putExtra("username", username);
showintent.putExtra("password", password);
startActivity(showintent);
Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
return;
}
}
break;
}
}
}
}
3.8显示页ShowActivity.java
package com.example.jh_android;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.litepal.LitePal;
import java.util.List;
public class ShowActivity extends AppCompatActivity {
private TextView showUsername;
private TextView showPassword;
private Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
showUsername = findViewById(R.id.show_username);
showPassword = findViewById(R.id.show_password);
delete = findViewById(R.id.btn_delete);
Intent intent = getIntent();
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
showUsername.setText("用户名 : "+username);
showPassword.setText("密 码 : "+password);
delete.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(ShowActivity.this);
builder.setTitle("警告框");
builder.setMessage("确定注销吗?");
builder.setPositiveButton("确定", (dialog, which) -> {
LitePal.deleteAll(User.class, "username=?", username);
LitePal.deleteAll(User.class, "password=?", password);
Intent showintent = new Intent(ShowActivity.this, LoginActivity.class);
startActivity(showintent);
Toast.makeText(ShowActivity.this, "注销成功 ", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) ->
Toast.makeText(ShowActivity.this, "注销失败" , Toast.LENGTH_SHORT).show());
builder.show();
});
}
}