利用litepal操作数据库的便利不必多说,本篇文章简要总结我在使用litepal实现APP注册登录功能的过程。
Litepal实现注册登录,包含三部分文件
1)数据库文件,包含表格文件、表格管理文件
2)Activity文件,注册、登录等
3)xml布局文件
1. 数据库文件:
1.1 User表格
这里定义了基本用户信息:用户名、密码、手机等。
public class User extends DataSupport {
private String userId;
private String userName;
private String userPwd;
private String userPhone;
private String userEmail;
public String getUserName() {
return userName; }
public void setUserName(String userName) {
this.userName = userName; }
public String getUserPhone() {
return userPhone; }
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;}
public String getUserPwd() {
return userPwd; }
public void setUserPwd(String userPwd) {
this.userPwd = userPwd; }
/*public String getUserEmail() {
return userEmail; }
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}*/
}
在litepal.xml文件中添加到映射模型列表:
1.2 数据表格管理文件
在这里定义保存、特定查询等函数。
public class UserManager {
//插入用户数据(注册用)
public boolean insertUser(String name,String password,String phone){
User user=new User();
user.setUserName(name);
user.setUserPwd(password);
user.setUserPhone(phone);
//user.setUserEmail(email);
//创建数据库
LitePal.getDatabase();
return user.save();
}
//查找用户名,查看用户是否存在(注册、找回密码用)
public boolean findUserByName(String name){
List users = DataSupport.findAll(User.class);
for(User person: users){
if(name.trim().equals(person.getUserName())){
return true;
}
}
return false;
}
//查找手机号是否已注册(找回密码用)
public boolean findByPhone(String phone){
List users = DataSupport.findAll(User.class);
for(User person: users){
if(phone.trim().equals(person.getUserPhone())){
return true;
}
}
return false;
}
//查找用户名、密码是否正确匹配(登录、重置密码用)
public boolean findNameAndPwd(String name,String password){
List users = DataSupport.findAll(User.class);
for(User person: users){
if((name.trim().equals(person.getUserName()))&&(password.trim().equals(person.getUserPwd()))){
return true;
}
}
return false;
}
}
2. Activity文件
!!一定要在AndroidManifest.xml文件中注册。
<activity android:name=".HomeActivity"></activity>
<activity android:name=".RegisterActivity"></activity>
登录、注册Activity文件其实都只包含三部分:1)变量定义实例化、2)按钮点击事件监测、3)信息判断(是否为空?是否合理?)
2.1 MainActivity文件(登录)
首先,是基本的变量定义与实例化:
public class MainActivity extends AppCompatActivity {
private Intent mHome, mRegister;
private Button btn_login, btn_register;
private EditText edt_name, edt_password;
UserManager userManager = new UserManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//隐藏标题栏
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
initView();
}
//获取实例对象
public void initView() {
edt_name = (EditText) findViewById(R.id.account_login);
edt_password = (EditText) findViewById(R.id.pass_login);
btn_register = (Button) findViewById(R.id.register_now);
btn_login = (Button) findViewById(R.id.loginButton);
btn_login.setOnClickListener(new click());
btn_register.setOnClickListener(new click());
}
对按钮点击进行监听:
//按钮点击事件监听
private class click implements View.OnClickListener{
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.register_now:
mRegister=new Intent(MainActivity.this,RegisterActivity.class);//跳转至注册界面
startActivity(mRegister);
break;
case R.id.loginButton:
login_judge();
break;
default:
break;
}
}
}
登录信息判断:
//对输入的用户名、密码进行验证,判断界面跳转方向
private void login_judge() {
//获取用户名、密码
boolean flag;
String name =edt_name.getText().toString().trim();
String pwd =edt_password.getText().toString().trim();
flag=userManager.findNameAndPwd(name, pwd);
if(flag){
Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
mHome = new Intent(MainActivity.this, HomeActivity.class); //跳转到首页
startActivity(mHome);
finish();
}
else{
Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
return;
}
}
}
2.2 RegisterActivity(注册)
变量定义与实例化:
public class RegisterActivity extends AppCompatActivity {
private EditText mAccount; //账户
private EditText mPwd; //密码
private EditText mPwdCheck; //确认密码
private EditText mPhone; //手机
private Button mSureButton;
UserManager userManager = new UserManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//隐藏标题栏
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.hide();
}
initView();
}
//获取实例对象
public void initView() {
mAccount = (EditText)findViewById(R.id.account_register);
mPwd = (EditText)findViewById(R.id.pass_register);
mPwdCheck = (EditText)findViewById(R.id.rePass_register);
mSureButton = (Button)findViewById(R.id.registerButton);
mPhone=(EditText)findViewById(R.id.ph_register);
mSureButton.setOnClickListener(new Click());
}
注册点击事件检测:
private class Click implements View.OnClickListener {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.registerButton:
register_check();
break;
default:
break;
}
}
}
注册信息检测:
//对注册信息进行验证,用户名是否已注册,两次密码输入是否一致private void register_check() {
if(isUserNameAndPwdValid()){
boolean flag;
String name =mAccount.getText().toString().trim();
String password =mPwd.getText().toString().trim();
String rePassword =mPwdCheck.getText().toString().trim();
String phone=mPhone.getText().toString().trim();
flag =userManager.findUserByName(name);
if(flag){
Toast.makeText(this, "用户名已经存在",Toast.LENGTH_SHORT).show();
return ;
}
if(!password.equals(rePassword)){
Toast.makeText(this, "输入的密码不同",Toast.LENGTH_SHORT).show();
}
else {
flag=userManager.insertUser(name,password,phone);
if(!flag){
Toast.makeText(this,"注册失败(信息保存失败)",Toast.LENGTH_LONG).show();
return ;
}
Toast.makeText(this,"注册成功",Toast.LENGTH_LONG).show();
Intent intent_Register_to_Login =new Intent(RegisterActivity.this,MainActivity.class) ; //切换RegisterActivity至Login Activity
startActivity(intent_Register_to_Login);
finish();
}
}
}
判断信息是否为空
//判断输入的昵称,密码,确认密码是否为空
public boolean isUserNameAndPwdValid() {
if (mPhone.getText().toString().trim().equals("")) {
Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
return false;
}
else if (mAccount.getText().toString().trim().equals("")) {
Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
return false;
}
else if (mPwd.getText().toString().trim().equals("")) {
Toast.makeText(this, "密码不能为空",Toast.LENGTH_SHORT).show();
return false;
}
else if(mPwdCheck.getText().toString().trim().equals("")) {
Toast.makeText(this, "再次输入密码不能为空", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
}
3. XML布局文件
3.1 activity_main.xml
这里我用了自定义的ImageView文件。
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background"
tools:context="com.example.administrator.test.MainActivity">
<TextView
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="#000000"
android:textSize="24sp"
android:id="@+id/textView_login"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<com.example.administrator.test.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:src="@drawable/app"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="@+id/imgLogin"/>
<EditText
android:drawableLeft="@android:drawable/ic_menu_myplaces"
android:layout_width="260dp"
android:layout_height="40dp"
android:digits="0123456789"
android:hint="请输入用户名"
android:maxLines="1"
android:id="@+id/account_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"/>
<EditText
android:drawableLeft="@android:drawable/ic_lock_idle_lock"
android:layout_width="260dp"
android:layout_height="40dp"
android:inputType="textPassword"
android:hint="密码"
android:maxLines="1"
android:id="@+id/pass_login"
android:layout_below="@+id/account_login"
android:layout_alignLeft="@+id/account_login"
android:layout_alignStart="@+id/account_login"
android:layout_marginTop="30dp"/>
<Button
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="60dp"
android:textColor="#000"
android:textSize="20dp"
android:text="登录"
android:id="@+id/loginButton"
android:layout_below="@+id/pass_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"/>
<Button
android:text="立即注册"
android:textColor="#04ba1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/register_now"
android:layout_marginBottom="59dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="27dp"
android:layout_marginEnd="27dp"/>
</RelativeLayout>
3.1 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"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background"
tools:context="com.example.administrator.test.MainActivity">
<TextView
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="#000000"
android:textSize="24sp"
android:id="@+id/textView_register"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<!-- 注册-->
<Button
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="60dp"
android:textColor="#000"
android:textSize="20dp"
android:text="注册"
android:id="@+id/registerButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp" />
<EditText
android:drawableLeft="@android:drawable/ic_menu_myplaces"
android:layout_width="260dp"
android:layout_height="40dp"
android:hint="请输入手机号"
android:maxLines="1"
android:id="@+id/ph_register"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<EditText
android:drawableLeft="@android:drawable/ic_menu_myplaces"
android:layout_width="260dp"
android:layout_height="40dp"
android:digits="0123456789"
android:hint="请输入用户名"
android:maxLines="1"
android:id="@+id/account_register"
android:layout_below="@+id/ph_register"
android:layout_alignLeft="@+id/ph_register"
android:layout_alignStart="@+id/ph_register"
android:layout_marginTop="30dp" />
<EditText
android:drawableLeft="@android:drawable/ic_lock_idle_lock"
android:layout_width="260dp"
android:layout_height="40dp"
android:inputType="textPassword"
android:hint="密码"
android:maxLines="1"
android:id="@+id/pass_register"
android:layout_below="@+id/account_register"
android:layout_alignLeft="@+id/account_register"
android:layout_alignStart="@+id/account_register"
android:layout_marginTop="30dp"/>
<EditText
android:drawableLeft="@android:drawable/ic_lock_idle_lock"
android:layout_width="260dp"
android:layout_height="40dp"
android:inputType="textPassword"
android:hint="确认密码"
android:maxLines="1"
android:id="@+id/rePass_register"
android:layout_below="@+id/pass_register"
android:layout_alignLeft="@+id/pass_register"
android:layout_alignStart="@+id/pass_register"
android:layout_marginTop="30dp" />
</RelativeLayout>
网友评论