【Android】家庭记账本手机版开发报告五
一、说在前面
昨天 |
1、添加菜单(查询、清除所有等)2、使用滑动删除 |
今天 |
1、创建登入和注册界面 2、向数据库添加一张用户表 |
问题 | 做完后在登入时有bug(未解决) |
二、界面的搭建
1、登入界面
2、登入界面的代码(粗糙版)
<?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:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".loginFragment" > <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.9" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title_text" android:textSize="@dimen/title_font" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline4" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="@+id/guideline6" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.05" /> <ImageView android:id="@+id/imageView" android:layout_width="175dp" android:layout_height="174dp" android:contentDescription="@string/dec" android:src="@drawable/login" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintDimensionRatio="w,1:1" app:layout_constraintEnd_toStartOf="@+id/guideline4" app:layout_constraintStart_toStartOf="@+id/guideline6" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.2" /> <EditText android:id="@+id/editTextusername" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:ems="10" android:hint="@string/username" android:importantForAutofill="no" android:inputType="textPersonName" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline4" app:layout_constraintStart_toStartOf="@+id/guideline6" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" /> <EditText android:id="@+id/editTextpassword" android:layout_width="0dp" android:layout_height="wrap_content" android:ems="10" android:hint="@string/password" android:importantForAutofill="no" android:inputType="textPassword" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/editTextusername" app:layout_constraintStart_toStartOf="@+id/editTextusername" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.6" /> <Button android:id="@+id/buttonlogin" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/login" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/editTextusername" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="@+id/editTextusername" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.7" /> <Button android:id="@+id/buttonregister" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/button_registe" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/editTextusername" app:layout_constraintStart_toStartOf="@+id/editTextusername" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.8" /> </androidx.constraintlayout.widget.ConstraintLayout>
3、注册界面
4、注册界面代码
<?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:id="@+id/frameLayout2" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UserAddFragment"> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/user_add_tltle" android:textSize="@dimen/title_font" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline10" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="@+id/guideline9" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.15" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.1" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.9" /> <EditText android:id="@+id/editTextU" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:hint="@string/user_add_username" android:inputType="textPersonName" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline10" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="@+id/guideline9" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3" android:importantForAutofill="no" /> <EditText android:id="@+id/editTextP" android:layout_width="0dp" android:layout_height="wrap_content" android:ems="10" android:hint="@string/user_add_password" android:inputType="textPassword" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/editTextU" app:layout_constraintStart_toStartOf="@+id/editTextU" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.45" android:importantForAutofill="no" /> <EditText android:id="@+id/editTextRP" android:layout_width="0dp" android:layout_height="wrap_content" android:ems="10" android:hint="@string/user_add_repressword" android:inputType="textPassword" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/editTextP" app:layout_constraintStart_toStartOf="@+id/editTextP" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.6" android:importantForAutofill="no" /> <Button android:id="@+id/buttonR" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/button_registe" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/editTextU" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="@+id/editTextU" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.75" /> </androidx.constraintlayout.widget.ConstraintLayout>
二、用户表的创建
1、实体(User.java)
package com.me.familybookkeepingbook; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class User { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "user_name") private String userName; @ColumnInfo(name = "password") private String password; @ColumnInfo(name = "current_money") private double currentMoney; public User(String userName, String password) { this.userName = userName; this.password = password; this.currentMoney = 0; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } public double getCurrentMoney() { return currentMoney; } public void setCurrentMoney(double currentMoney) { this.currentMoney = currentMoney; } }
2、Dao
package com.me.familybookkeepingbook; import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @Dao public interface UserDao { @Insert void insertUser(User...users); @Delete void deleteUser(User...users); @Update void updateUser(User...users); @Query("SELECT * From USER WHERE user_name like:userName_y AND password like :password_y") User findUser(String userName_y ,String password_y); }
3、DataBase
package com.me.familybookkeepingbook; import androidx.room.Database; import android.content.Context; import androidx.room.Room; import androidx.room.RoomDatabase; @Database(entities = {User.class},version = 1,exportSchema = false) public abstract class UserDatabase extends RoomDatabase { private static UserDatabase INSTANCE; public abstract UserDao getUserDao(); static synchronized UserDatabase getUserDataBase(Context context){ if (INSTANCE == null){ INSTANCE = Room.databaseBuilder(context.getApplicationContext(),UserDatabase.class,"user_database") .build(); } return INSTANCE; } }
4、工厂
package com.me.familybookkeepingbook; import android.content.Context; import android.os.AsyncTask; import androidx.lifecycle.LiveData; import java.util.List; public class UserRepository { private UserDao userDao; static User user_0; public UserRepository(Context context) { UserDatabase userDatabase = UserDatabase.getUserDataBase(context.getApplicationContext()); userDao = userDatabase.getUserDao(); } void insertUser(User ... users){ new InsertAsyncTask(userDao).execute(users); } void deleteUser(User ... users){ new DeleteAsyncTask(userDao).execute(users); } void updateUser(User ... users){ new UpdateAsyncTask(userDao).execute(users); } Boolean queueUser(User user){ new QueueAsyncTask(userDao).execute(user); if(user==null||user_0==null){ return false; }else if(user_0.getUserName().equals(user.getUserName())&&user_0.getPassword().equals(user.getPassword())){ return true; }else { return false; } } static class InsertAsyncTask extends AsyncTask<User ,Void,Void> { private UserDao userDao; public InsertAsyncTask(UserDao userDao) { this.userDao = userDao; } @Override protected Void doInBackground(User... users) { userDao.insertUser(users); return null; } } static class DeleteAsyncTask extends AsyncTask<User ,Void,Void> { private UserDao userDao; public DeleteAsyncTask(UserDao userDao) { this.userDao = userDao; } @Override protected Void doInBackground(User... users) { userDao.deleteUser(users); return null; } } static class UpdateAsyncTask extends AsyncTask<User ,Void,Void> { private UserDao userDao; public UpdateAsyncTask(UserDao userDao) { this.userDao = userDao; } @Override protected Void doInBackground(User... users) { userDao.updateUser(users); return null; } } static class QueueAsyncTask extends AsyncTask<User ,Void,Void> { private UserDao userDao; public QueueAsyncTask(UserDao userDao) { this.userDao = userDao; } @Override protected Void doInBackground(User... users) { User user1 = new User("王正帅","123"); for (User user:users) { user1 = userDao.findUser(user.getUserName(),user.getPassword()); user_0 = user1; } return null; } } }
三、功能逻辑
1、注册
1)点击注册从登入转到注册界面
buttonRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NavController controller = Navigation.findNavController(v); controller.navigate(R.id.action_loginFragment_to_userAddFragment); } });
2)将正确数据添加导数据库并返回登录界面
@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); userRepository = new UserRepository(requireActivity()); buttonRegister = requireActivity().findViewById(R.id.buttonR); editTextPassword = requireActivity().findViewById(R.id.editTextP); editTextRP = requireActivity().findViewById(R.id.editTextRP); editTextUserName = requireActivity().findViewById(R.id.editTextU); buttonRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName = editTextUserName.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); User user = new User(userName,password); userRepository.insertUser(user); NavController controller = Navigation.findNavController(v); controller.navigate(R.id.action_userAddFragment_to_loginFragment); } }); }
2、登入
buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName = editTextUserName.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); User user = new User(userName,password); Boolean f = userRepository.queueUser(user); if(f){ NavController controller = Navigation.findNavController(v); controller.navigate(R.id.action_loginFragment_to_accountRecordFragment); }else { AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); builder.setTitle("账号或密码错误,请重新输入!"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create(); builder.show(); } } });
四、运行测试