diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml index d9b9424..884727e 100644 --- a/.idea/dbnavigator.xml +++ b/.idea/dbnavigator.xml @@ -6,7 +6,7 @@ - + @@ -17,6 +17,21 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/java/cc/liuyx/app/LoginActivity.java b/app/src/main/java/cc/liuyx/app/LoginActivity.java index fea5365..f9a5ecc 100644 --- a/app/src/main/java/cc/liuyx/app/LoginActivity.java +++ b/app/src/main/java/cc/liuyx/app/LoginActivity.java @@ -9,16 +9,27 @@ import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; +import androidx.room.Room; + +import cc.liuyx.app.dao.UserDao; +import cc.liuyx.app.db.UserDB; +import cc.liuyx.app.entity.User; public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private EditText usernameEdit, passwdEdit; + private static UserDB db; + private UserDao userDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); + db = Room.databaseBuilder(getApplicationContext(), UserDB.class, "user") + .allowMainThreadQueries() + .build(); + usernameEdit = findViewById(R.id.login_username); passwdEdit = findViewById(R.id.login_passwd); @@ -39,22 +50,36 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList } } + @Override + protected void onStop() { + super.onStop(); + db.close(); + } + private void login() { String username = usernameEdit.getText().toString(); String passwd = passwdEdit.getText().toString(); + userDao = db.getUserDao(); + if (!username.equals("") && !passwd.equals("")) { - SharedPreferences sp = getSharedPreferences("loginStatus", MODE_PRIVATE); - String localName = sp.getString("username", null); - String localPasswd = sp.getString("passwd", null); - if (!username.equals(localName) || !passwd.equals(localPasswd)) { - Toast.makeText(LoginActivity.this, "用户名或密码错误>_<", Toast.LENGTH_SHORT).show(); + // 在数据库中查询有无,如果有,则保存其登陆状态和信息 + User user = userDao.findUserByName(username); + if (user == null) { + Toast.makeText(LoginActivity.this, "该用户还没有注册哦>_<", Toast.LENGTH_SHORT).show(); + return; + } + if (!username.equals(user.getName()) || !passwd.equals(user.getPasswd())) { + Toast.makeText(LoginActivity.this, "您输入的密码有误", Toast.LENGTH_SHORT).show(); return; } + SharedPreferences sp = getSharedPreferences("loginStatus", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("isLoggedIn", true); - editor.putString("username", username); + editor.putString("username", user.getName()); + editor.putString("email", user.getEmail()); + editor.putString("phone", user.getPhone()); editor.apply(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); @@ -63,7 +88,5 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList } else { Toast.makeText(LoginActivity.this, "请填入用户名与密码哦~", Toast.LENGTH_SHORT).show(); } - - } } \ No newline at end of file diff --git a/app/src/main/java/cc/liuyx/app/RegisterActivity.java b/app/src/main/java/cc/liuyx/app/RegisterActivity.java index ad0f5d4..a5ac700 100644 --- a/app/src/main/java/cc/liuyx/app/RegisterActivity.java +++ b/app/src/main/java/cc/liuyx/app/RegisterActivity.java @@ -10,6 +10,11 @@ import android.widget.ImageView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; +import androidx.room.Room; + +import cc.liuyx.app.dao.UserDao; +import cc.liuyx.app.db.UserDB; +import cc.liuyx.app.entity.User; public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { @@ -20,12 +25,19 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL private String email = ""; private EditText usernameEdit, passwdEdit, passwdAgainEdit, phoneEdit, emailEdit; + private UserDao userDao; + private UserDB db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); + db = Room.databaseBuilder(getApplicationContext(), UserDB.class, "user") + .allowMainThreadQueries() + .build(); + userDao = db.getUserDao(); + // 初始化每个组件 usernameEdit = findViewById(R.id.username); passwdEdit = findViewById(R.id.passwd); @@ -45,6 +57,12 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL }); } + @Override + protected void onStop() { + super.onStop(); + db.close(); + } + @Override public void onClick(View v) { if (v.getId() == R.id.btn_register) { @@ -64,6 +82,14 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL // 判断两次密码是否相等 if (passwd.equals(passwdAgain)) { if (!username.equals("") && !passwd.equals("")) { + // 向数据库中插入新用户的数据 + // 如果用户名重复,则此次操作不被允许 + if (userDao.findUserByName(username) != null) { + Toast.makeText(RegisterActivity.this, "已经存在该用户名啦", Toast.LENGTH_SHORT).show(); + return; + } + userDao.insert(new User(username, passwd, phone, email)); + // 设置登录状态 SharedPreferences sharedPreferences = getSharedPreferences("loginStatus", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); @@ -71,7 +97,6 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL editor.putString("username", username); editor.putString("email", email); editor.putString("phone", phone); - editor.putString("passwd", passwd); editor.apply(); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); diff --git a/app/src/main/java/cc/liuyx/app/dao/UserDao.java b/app/src/main/java/cc/liuyx/app/dao/UserDao.java index cbe0037..5b7d8dd 100644 --- a/app/src/main/java/cc/liuyx/app/dao/UserDao.java +++ b/app/src/main/java/cc/liuyx/app/dao/UserDao.java @@ -1,10 +1,30 @@ package cc.liuyx.app.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; +import androidx.room.Query; +import androidx.room.Update; + +import java.util.List; + +import cc.liuyx.app.entity.User; @Dao public interface UserDao { @Insert - void insert(); + void insert(User... users); + + @Delete + void delete(User user); + + @Update + void update(User user); + + @Query("select * from user") + List selectAllUser(); + + @Query("select * from user where name = :name") + User findUserByName(String name); + } diff --git a/app/src/main/java/cc/liuyx/app/db/UserDB.java b/app/src/main/java/cc/liuyx/app/db/UserDB.java new file mode 100644 index 0000000..4f6a20e --- /dev/null +++ b/app/src/main/java/cc/liuyx/app/db/UserDB.java @@ -0,0 +1,12 @@ +package cc.liuyx.app.db; + +import androidx.room.Database; +import androidx.room.RoomDatabase; + +import cc.liuyx.app.dao.UserDao; +import cc.liuyx.app.entity.User; + +@Database(entities = User.class, version = 1, exportSchema = false) +public abstract class UserDB extends RoomDatabase { + public abstract UserDao getUserDao(); +} diff --git a/app/src/main/java/cc/liuyx/app/entity/User.java b/app/src/main/java/cc/liuyx/app/entity/User.java index 8f51de8..11ac6b2 100644 --- a/app/src/main/java/cc/liuyx/app/entity/User.java +++ b/app/src/main/java/cc/liuyx/app/entity/User.java @@ -18,6 +18,29 @@ public class User { @ColumnInfo(name = "email") private String email; + public User() { + } + + public User(String name, String passwd, String phone, String email) { + this.name = name; + this.passwd = passwd; + this.phone = phone; + this.email = email; + } + + public User(String name, String passwd) { + this.name = name; + this.passwd = passwd; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public String getName() { return name; }