当前位置:   article > 正文

Android Studio实现Mysql(5.7)数据库增删改查(下)——用Bcrypt Hash实现数据库密码加密及验证,展示用户条目并实现增删改查_android bcrypt

android bcrypt

目录

用Java实现移动端(Android)哈希Bcrypt加密

完整增删改查案例(自定义适配器、listview等)


来得晚的小伙伴们请先看上集:(35条消息) Android Studio实现Mysql(5.7)数据库增删改查(上)——用户登录功能实现_TidesWise的博客-CSDN博客

用Java实现移动端(Android)哈希Bcrypt加密

1、在libs文件夹下添加jbcrypt-0.4.jar包,具体资源自行互联网下载,再右键add as Library,具体操作参见上集;

2、在CommonUtils添加以下两个方法,用以得到哈希加密的密码和验证用户输入密码是否与数据库中加密密码一致;注意引入jbcrypt:

  1. import org.mindrot.jbcrypt.BCrypt;
  2. public static String getBcryptHash(String spassword) {
  3. String encodePwd = BCrypt.hashpw(spassword, BCrypt.gensalt()); // 加密,核心代码
  4. return encodePwd;
  5. }
  6. public static boolean checkPw(String spassword, String hashedPw) {
  7. boolean flag = BCrypt.checkpw(spassword, hashedPw); // 验证加密是否正确
  8. return flag;
  9. }

3、淘汰StudentDao.java类中的getUserByLogin方法,新增getUserBySid方法;

  1. public StudentInfo getUserBySid(String sid) {
  2. StudentInfo item = null;
  3. try {
  4. getConnection();
  5. String sql = "select * from student where sid=?";
  6. pStmt = conn.prepareStatement(sql);
  7. pStmt.setString(1, sid);
  8. rs = pStmt.executeQuery();
  9. if (rs.next()) {
  10. item = new StudentInfo();
  11. item.setSid(sid);
  12. item.setSpassword(rs.getString("spassword"));
  13. item.setSname(rs.getString("sname"));
  14. item.setSgender(rs.getString("gender"));
  15. item.setSlocation(rs.getString("slocation"));
  16. item.setSphone(rs.getString("sphone"));
  17. item.setSemail(rs.getString("semail"));
  18. item.setCid(rs.getString("cid"));
  19. }
  20. } catch (Exception ex) {
  21. ex.printStackTrace();
  22. } finally {
  23. closeAll();
  24. }
  25. return item;
  26. }

4、自此在登陆操作时,我们只需通过getUserBySid方法获取数据库中加密的密码,再通过CommonUtils类中方法checkPw比较决定用户是否登陆操作成功;修改MainActivity.java中的doLogin方法如下所示:

  1. private void doLogin() {
  2. final String sid = txtSid.getText().toString().trim();
  3. final String spassword = txtSpassword.getText().toString().trim();
  4. if (TextUtils.isEmpty(sid)) {
  5. CommonUtils.showShortMsg(this, "请输入用户名");
  6. txtSid.requestFocus();
  7. } else if (TextUtils.isEmpty(spassword)) {
  8. CommonUtils.showShortMsg(this, "请输入密码");
  9. txtSpassword.requestFocus();
  10. } else {
  11. new Thread(new Runnable() {
  12. @Override //重写run方法
  13. public void run() {
  14. final StudentInfo item;
  15. if (dao.getUserBySid(sid) == null) //检验是否有此用户账号
  16. item = null;
  17. else if (CommonUtils.checkPw(spassword, dao.getUserBySid(sid).getSpassword())) //检验用户输入密码是否与数据库存储的加密密码一致
  18. item = dao.getUserBySid(sid);
  19. else item = null; //不一致的情况
  20. mainHandler.post(new Runnable() {
  21. @Override
  22. public void run() {
  23. if (item == null)
  24. CommonUtils.showDialogMsg(MainActivity.this, "用户名或密码错误");
  25. else
  26. CommonUtils.showDialogMsg(MainActivity.this, "登录成功");
  27. }
  28. });
  29. }
  30. }).start(); //开启线程
  31. }
  32. }

完整增删改查案例(自定义适配器、listview等)

1、新建StudentManagerActivity,在阿里巴巴矢量图标库网站下载返回图标return.png添加到res文件夹的drawable中;TIPS:本文最后会粘贴所有源码,以下讲述思路

2、注意修改数据库时出现的中文乱码问题,在DBOpenHelper中修改:

private static final String URL = "jdbc:mysql://10.32.27.173/njtech?characterEncoding=utf8"; //输入IPV4地址以及要查询的数据库

2、新建layout XML File:student_list_item.xml,用于展示listview:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_margin="5dp"
  6. android:orientation="vertical">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal">
  11. <TextView
  12. android:id="@+id/txtSindex"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="1."
  16. android:textColor="#9C27B0"
  17. android:textSize="16sp"
  18. android:textStyle="bold"></TextView>
  19. <TextView
  20. android:id="@+id/txtSid"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_marginLeft="5dp"
  24. android:text="202021136021"
  25. android:textColor="#9C27B0"
  26. android:textSize="16sp"
  27. android:textStyle="bold"></TextView>
  28. <TextView
  29. android:id="@+id/txtSname"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginLeft="5dp"
  33. android:text="XXX"
  34. android:textColor="#9C27B0"
  35. android:textSize="16sp"
  36. android:textStyle="bold"></TextView>
  37. <LinearLayout
  38. android:layout_width="match_parent"
  39. android:layout_height="30dp"
  40. android:gravity="right">
  41. <ImageView
  42. android:id="@+id/btnEdit"
  43. android:layout_width="30dp"
  44. android:layout_height="30dp"
  45. android:layout_marginRight="10dp"
  46. android:src="@drawable/edit"></ImageView>
  47. <ImageView
  48. android:id="@+id/btnDelete"
  49. android:layout_width="30dp"
  50. android:layout_height="30dp"
  51. android:layout_marginRight="10dp"
  52. android:src="@drawable/delete"></ImageView>
  53. </LinearLayout>
  54. </LinearLayout>
  55. </LinearLayout>

3、创建接口OnEditBtnClickListener和OnDeleteBtnClickListener :

  1. package com.example.myapplication;
  2. import android.view.View;
  3. /*
  4. * 修改按钮点击事件监听接口
  5. */
  6. public interface OnEditBtnClickListener {
  7. void onEditBtnClick(View view, int position);
  8. }
  1. package com.example.myapplication;
  2. /*删除按钮点击事件*/
  3. import android.view.View;
  4. public interface OnDeleteBtnClickListener {
  5. void onDeleteBtnClick(View view, int position);
  6. }

4、创建自定义用户适配器类LvStudentInfoAdapter :

  1. package com.example.myapplication;
  2. /*自定义用户适配器类*/
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import java.util.List;
  11. public class LvStudentInfoAdapter extends BaseAdapter {
  12. private Context context;
  13. private List<StudentInfo> studentInfoList;
  14. private OnEditBtnClickListener onEditBtnClickListener;
  15. private OnDeleteBtnClickListener onDeleteBtnClickListener;
  16. public LvStudentInfoAdapter() {
  17. }
  18. public LvStudentInfoAdapter(Context context, List<StudentInfo> studentInfoList) {
  19. this.context = context;
  20. this.studentInfoList = studentInfoList;
  21. }
  22. public void setStudentInfoList(List<StudentInfo> studentInfoList) {
  23. this.studentInfoList = studentInfoList;
  24. }
  25. public void setOnEditBtnClickListener(OnEditBtnClickListener onEditBtnClickListener) {
  26. this.onEditBtnClickListener = onEditBtnClickListener;
  27. }
  28. public void setOnDeleteBtnClickListener(OnDeleteBtnClickListener onDeleteBtnClickListener) {
  29. this.onDeleteBtnClickListener = onDeleteBtnClickListener;
  30. }
  31. @Override
  32. public int getCount() {
  33. return studentInfoList.size();
  34. }
  35. @Override
  36. public Object getItem(int i) {
  37. return studentInfoList.get(i);
  38. }
  39. @Override
  40. public long getItemId(int i) {
  41. return i;
  42. }
  43. @Override
  44. public View getView(int i, View convertView, ViewGroup viewGroup) {
  45. // return null;
  46. ViewHolder viewHolder = null;
  47. if (convertView == null) {
  48. convertView = LayoutInflater.from(context).inflate(R.layout.student_list_item, null);
  49. viewHolder = new ViewHolder();
  50. viewHolder.txtSid = convertView.findViewById(R.id.txtSid);
  51. viewHolder.txtSindex = convertView.findViewById(R.id.txtSindex);
  52. viewHolder.txtSname = convertView.findViewById(R.id.txtSname);
  53. viewHolder.btnEdit = convertView.findViewById(R.id.btnEdit);
  54. viewHolder.btnDelete = convertView.findViewById(R.id.btnDelete);
  55. convertView.setTag(viewHolder);
  56. } else {
  57. viewHolder = (ViewHolder) convertView.getTag();
  58. }
  59. StudentInfo item = studentInfoList.get(i);
  60. viewHolder.txtSindex.setText(i + 1 + ".");
  61. viewHolder.txtSid.setText(item.getSid());
  62. viewHolder.txtSname.setText(item.getSname());
  63. viewHolder.btnEdit.setOnClickListener(new View.OnClickListener() {
  64. @Override
  65. public void onClick(View view) {
  66. onEditBtnClickListener.onEditBtnClick(view, i);
  67. }
  68. });
  69. viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View view) {
  72. onDeleteBtnClickListener.onDeleteBtnClick(view, i);
  73. }
  74. });
  75. return convertView;
  76. }
  77. //自定义内部类
  78. private class ViewHolder {
  79. private TextView txtSindex, txtSid, txtSname;
  80. private ImageView btnEdit, btnDelete;
  81. }
  82. }

5、创建StudentManagerActivity:

  1. package com.example.myapplication;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.ImageView;
  12. import android.widget.ListView;
  13. import java.util.List;
  14. public class StudentManagerActivity extends AppCompatActivity implements View.OnClickListener {
  15. private ImageView btnReturn, btnAdd;
  16. private ListView lvStudent;
  17. private Handler mainHandler;
  18. private StudentDao studentDao; //用户数据库操作实例
  19. private List<StudentInfo> studentInfoList;
  20. private LvStudentInfoAdapter lvStudentInfoAdapter; //用户信息数据适配器
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_student_manager);
  25. initView();
  26. loadStudentList();
  27. }
  28. private void initView() {
  29. studentDao = new StudentDao();
  30. btnReturn = findViewById(R.id.btnReturn);
  31. btnAdd = findViewById(R.id.btnAdd);
  32. lvStudent = findViewById(R.id.lvStudent);
  33. mainHandler = new Handler(getMainLooper());
  34. btnReturn.setOnClickListener(this);
  35. btnAdd.setOnClickListener(this);
  36. }
  37. private void loadStudentList() {
  38. new Thread(new Runnable() {
  39. @Override
  40. public void run() {
  41. studentInfoList = studentDao.getStudentList();
  42. mainHandler.post(new Runnable() {
  43. @Override
  44. public void run() {
  45. showLvData();
  46. }
  47. });
  48. }
  49. }).start();
  50. }
  51. private void showLvData() {
  52. if (lvStudentInfoAdapter == null) {
  53. lvStudentInfoAdapter = new LvStudentInfoAdapter(this, studentInfoList);
  54. lvStudent.setAdapter(lvStudentInfoAdapter);
  55. } else {
  56. lvStudentInfoAdapter.setStudentInfoList(studentInfoList);
  57. lvStudentInfoAdapter.notifyDataSetChanged();
  58. }
  59. //修改按钮
  60. lvStudentInfoAdapter.setOnEditBtnClickListener(new OnEditBtnClickListener() {
  61. @Override
  62. public void onEditBtnClick(View view, int position) {
  63. StudentInfo item = studentInfoList.get(position);
  64. Bundle bundle = new Bundle();
  65. bundle.putSerializable("item", item);
  66. Intent intent = new Intent(StudentManagerActivity.this, StudentEditActivity.class);
  67. intent.putExtras(bundle);
  68. startActivityForResult(intent, 1);
  69. }
  70. });
  71. //删除按钮
  72. lvStudentInfoAdapter.setOnDeleteBtnClickListener(new OnDeleteBtnClickListener() {
  73. @Override
  74. public void onDeleteBtnClick(View view, int position) {
  75. final String sid = studentInfoList.get(position).getSid();
  76. AlertDialog.Builder builder = new AlertDialog.Builder(StudentManagerActivity.this);
  77. builder.setTitle("提示信息");
  78. builder.setMessage("您确定要删除学生用户" + studentInfoList.get(position).getSname() + "的信息吗?");
  79. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  80. @Override
  81. public void onClick(DialogInterface dialogInterface, int i) {
  82. new Thread(new Runnable() {
  83. @Override
  84. public void run() {
  85. studentDao.delStudent(sid);
  86. mainHandler.post(new Runnable() {
  87. @Override
  88. public void run() {
  89. loadStudentList();
  90. }
  91. });
  92. }
  93. }).start();
  94. }
  95. });
  96. builder.setNegativeButton("取消", null);
  97. builder.create().show();
  98. }
  99. });
  100. }
  101. @Override
  102. public void onClick(View view) {
  103. switch (view.getId()) {
  104. case R.id.btnReturn:
  105. finish(); //结束当前页面
  106. break;
  107. case R.id.btnAdd:
  108. Intent intent = new Intent(this, StudentAddActivity.class);
  109. startActivityForResult(intent, 1);//requestCode
  110. break;
  111. }
  112. }
  113. @Override
  114. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  115. super.onActivityResult(requestCode, resultCode, data);
  116. if (requestCode == 1 && resultCode == 1) {
  117. loadStudentList();
  118. }
  119. }
  120. }

修改其样式文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:layout_margin="5dp"
  8. android:orientation="vertical"
  9. tools:context=".StudentManagerActivity">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:orientation="vertical">
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:orientation="horizontal">
  18. <ImageView
  19. android:id="@+id/btnReturn"
  20. android:layout_width="40dp"
  21. android:layout_height="40dp"
  22. android:src="@drawable/goback" />
  23. <ImageView
  24. android:id="@+id/btnAdd"
  25. android:layout_width="40dp"
  26. android:layout_height="40dp"
  27. android:layout_alignParentRight="true"
  28. android:src="@drawable/add" />
  29. </LinearLayout>
  30. <ListView
  31. android:id="@+id/lvStudent"
  32. android:layout_width="fill_parent"
  33. android:layout_height="fill_parent"
  34. android:paddingTop="0dp" />
  35. </LinearLayout>
  36. </LinearLayout>

6、创建StudentAddActivity:

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.text.TextUtils;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. public class StudentAddActivity extends AppCompatActivity {
  10. private EditText txtSid, txtSpassword, txtSname, txtSgender, txtSlocation, txtSphone, txtSemail, txtCid;
  11. private Handler mainHandler;
  12. private StudentDao studentDao;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_student_add);
  17. initView();
  18. }
  19. private void initView() {
  20. txtSid = findViewById(R.id.txtSid);
  21. txtSpassword = findViewById(R.id.txtSpassword);
  22. txtSname = findViewById(R.id.txtSname);
  23. txtSgender = findViewById(R.id.txtSgender);
  24. txtSlocation = findViewById(R.id.txtSlocation);
  25. txtSphone = findViewById(R.id.txtSphone);
  26. txtSemail = findViewById(R.id.txtSemail);
  27. txtCid = findViewById(R.id.txtCid);
  28. mainHandler = new Handler(getMainLooper());
  29. studentDao = new StudentDao();
  30. }
  31. public void btnAddClick(View view) {
  32. final String sid = txtSid.getText().toString().trim();
  33. final String spassword = txtSpassword.getText().toString().trim();
  34. final String sname = txtSname.getText().toString().trim();
  35. final String sgender = txtSgender.getText().toString().trim();
  36. final String slocaltion = txtSlocation.getText().toString().trim();
  37. final String sphone = txtSphone.getText().toString().trim();
  38. final String semail = txtSemail.getText().toString().trim();
  39. final String cid = txtCid.getText().toString().trim();
  40. if (TextUtils.isEmpty(sid) || TextUtils.isEmpty(spassword) || TextUtils.isEmpty(sname) || TextUtils.isEmpty(sgender) || TextUtils.isEmpty(slocaltion) || TextUtils.isEmpty(sphone) || TextUtils.isEmpty(semail) || TextUtils.isEmpty(cid)) {
  41. CommonUtils.showShortMsg(this, "请输入完整信息");
  42. // txtSid.requestFocus();
  43. } else {
  44. new Thread(new Runnable() {
  45. @Override //重写run方法
  46. public void run() {
  47. final StudentInfo item;
  48. if (studentDao.getStudentBySid(sid) != null) //检验是否有此用户账号
  49. item = null;
  50. else {
  51. item = new StudentInfo();
  52. item.setSid(sid);
  53. item.setSpassword(spassword);
  54. item.setSname(sname);
  55. item.setSgender(sgender);
  56. item.setSlocation(slocaltion);
  57. item.setSphone(sphone);
  58. item.setSemail(semail);
  59. item.setCid(cid);
  60. studentDao.addStudent(item);
  61. }
  62. mainHandler.post(new Runnable() {
  63. @Override
  64. public void run() {
  65. if (item == null)
  66. CommonUtils.showShortMsg(StudentAddActivity.this, "学号已存在");
  67. else {
  68. CommonUtils.showShortMsg(StudentAddActivity.this, "添加成功");
  69. // CommonUtils.showDialogMsg(MainActivity.this, "登录成功");
  70. setResult(1); //resultCode
  71. finish();
  72. // Intent intent = new Intent(StudentAddActivity.this, StudentManagerActivity.class);
  73. // startActivity(intent);
  74. }
  75. }
  76. });
  77. }
  78. }).start(); //开启线程
  79. }
  80. }
  81. }

修改其样式文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".StudentAddActivity">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:orientation="horizontal">
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="学 号:"
  17. android:textColor="#00f"
  18. android:textSize="16sp"
  19. android:textStyle="bold"></TextView>
  20. <EditText
  21. android:id="@+id/txtSid"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:background="@null"
  25. android:hint="请输入学号"
  26. android:inputType="textPersonName"
  27. android:textColor="#00f"
  28. android:textSize="16sp"
  29. android:textStyle="bold"></EditText>
  30. </LinearLayout>
  31. <LinearLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:orientation="horizontal">
  35. <TextView
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="密 码:"
  39. android:textColor="#00f"
  40. android:textSize="16sp"
  41. android:textStyle="bold"></TextView>
  42. <EditText
  43. android:id="@+id/txtSpassword"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:background="@null"
  47. android:hint="请输入密码"
  48. android:inputType="textPassword"
  49. android:textColor="#00f"
  50. android:textSize="16sp"
  51. android:textStyle="bold"></EditText>
  52. </LinearLayout>
  53. <LinearLayout
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:orientation="horizontal">
  57. <TextView
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:text="姓 名:"
  61. android:textColor="#00f"
  62. android:textSize="16sp"
  63. android:textStyle="bold"></TextView>
  64. <EditText
  65. android:id="@+id/txtSname"
  66. android:layout_width="match_parent"
  67. android:layout_height="wrap_content"
  68. android:background="@null"
  69. android:hint="请输入姓名"
  70. android:inputType="textPersonName"
  71. android:textColor="#00f"
  72. android:textSize="16sp"
  73. android:textStyle="bold"></EditText>
  74. </LinearLayout>
  75. <LinearLayout
  76. android:layout_width="match_parent"
  77. android:layout_height="wrap_content"
  78. android:orientation="horizontal">
  79. <TextView
  80. android:layout_width="wrap_content"
  81. android:layout_height="wrap_content"
  82. android:text="性 别:"
  83. android:textColor="#00f"
  84. android:textSize="16sp"
  85. android:textStyle="bold"></TextView>
  86. <EditText
  87. android:id="@+id/txtSgender"
  88. android:layout_width="match_parent"
  89. android:layout_height="wrap_content"
  90. android:background="@null"
  91. android:hint="请输入性别"
  92. android:inputType="textPersonName"
  93. android:textColor="#00f"
  94. android:textSize="16sp"
  95. android:textStyle="bold"></EditText>
  96. </LinearLayout>
  97. <LinearLayout
  98. android:layout_width="match_parent"
  99. android:layout_height="wrap_content"
  100. android:orientation="horizontal">
  101. <TextView
  102. android:layout_width="wrap_content"
  103. android:layout_height="wrap_content"
  104. android:text="地 址:"
  105. android:textColor="#00f"
  106. android:textSize="16sp"
  107. android:textStyle="bold"></TextView>
  108. <EditText
  109. android:id="@+id/txtSlocation"
  110. android:layout_width="match_parent"
  111. android:layout_height="wrap_content"
  112. android:background="@null"
  113. android:hint="请输入地址"
  114. android:inputType="textPostalAddress"
  115. android:textColor="#00f"
  116. android:textSize="16sp"
  117. android:textStyle="bold"></EditText>
  118. </LinearLayout>
  119. <LinearLayout
  120. android:layout_width="match_parent"
  121. android:layout_height="wrap_content"
  122. android:orientation="horizontal">
  123. <TextView
  124. android:layout_width="wrap_content"
  125. android:layout_height="wrap_content"
  126. android:text="手 机:"
  127. android:textColor="#00f"
  128. android:textSize="16sp"
  129. android:textStyle="bold"></TextView>
  130. <EditText
  131. android:id="@+id/txtSphone"
  132. android:layout_width="match_parent"
  133. android:layout_height="wrap_content"
  134. android:background="@null"
  135. android:hint="请输入手机号"
  136. android:inputType="phone"
  137. android:textColor="#00f"
  138. android:textSize="16sp"
  139. android:textStyle="bold"></EditText>
  140. </LinearLayout>
  141. <LinearLayout
  142. android:layout_width="match_parent"
  143. android:layout_height="wrap_content"
  144. android:orientation="horizontal">
  145. <TextView
  146. android:layout_width="wrap_content"
  147. android:layout_height="wrap_content"
  148. android:text="邮 箱:"
  149. android:textColor="#00f"
  150. android:textSize="16sp"
  151. android:textStyle="bold"></TextView>
  152. <EditText
  153. android:id="@+id/txtSemail"
  154. android:layout_width="match_parent"
  155. android:layout_height="wrap_content"
  156. android:background="@null"
  157. android:hint="请输入邮箱"
  158. android:inputType="textEmailAddress"
  159. android:textColor="#00f"
  160. android:textSize="16sp"
  161. android:textStyle="bold"></EditText>
  162. </LinearLayout>
  163. <LinearLayout
  164. android:layout_width="match_parent"
  165. android:layout_height="wrap_content"
  166. android:orientation="horizontal">
  167. <TextView
  168. android:layout_width="wrap_content"
  169. android:layout_height="wrap_content"
  170. android:text="班级id:"
  171. android:textColor="#00f"
  172. android:textSize="16sp"
  173. android:textStyle="bold"></TextView>
  174. <EditText
  175. android:id="@+id/txtCid"
  176. android:layout_width="match_parent"
  177. android:layout_height="wrap_content"
  178. android:background="@null"
  179. android:hint="请输入班级id"
  180. android:inputType="textPersonName"
  181. android:textColor="#00f"
  182. android:textSize="16sp"
  183. android:textStyle="bold"></EditText>
  184. </LinearLayout>
  185. <Button
  186. android:layout_width="match_parent"
  187. android:layout_height="wrap_content"
  188. android:onClick="btnAddClick"
  189. android:text="确 定"
  190. android:textColor="#0f0"
  191. android:textSize="18sp"
  192. android:textStyle="bold"></Button>
  193. </LinearLayout>

7、创建StudentEditActivity:

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. public class StudentEditActivity extends AppCompatActivity {
  9. private EditText txtSid, txtSpassword, txtSname, txtSgender, txtSlocation, txtSphone, txtSemail, txtCid;
  10. private Handler mainHandler;
  11. private StudentDao studentDao;
  12. private StudentInfo item;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_student_edit);
  17. initView();
  18. }
  19. private void initView() {
  20. Bundle bundle = getIntent().getExtras();
  21. txtSid = findViewById(R.id.txtSid);
  22. txtSpassword = findViewById(R.id.txtSpassword);
  23. txtSname = findViewById(R.id.txtSname);
  24. txtSgender = findViewById(R.id.txtSgender);
  25. txtSlocation = findViewById(R.id.txtSlocation);
  26. txtSphone = findViewById(R.id.txtSphone);
  27. txtSemail = findViewById(R.id.txtSemail);
  28. txtCid = findViewById(R.id.txtCid);
  29. if (bundle != null) {
  30. item = (StudentInfo) bundle.getSerializable("item");
  31. txtSid.setText(item.getSid());
  32. // txtSpassword.setText(item.getSpassword());
  33. txtSname.setText(item.getSname());
  34. txtSgender.setText(item.getSgender());
  35. txtSlocation.setText(item.getSlocation());
  36. txtSphone.setText(item.getSphone());
  37. txtSemail.setText(item.getSemail());
  38. txtCid.setText(item.getCid());
  39. }
  40. mainHandler = new Handler(getMainLooper());
  41. studentDao = new StudentDao();
  42. }
  43. public void btnEditClick(View view) {
  44. final String sid = txtSid.getText().toString().trim();
  45. final String spassword = txtSpassword.getText().toString().trim();
  46. final String sname = txtSname.getText().toString().trim();
  47. final String sgender = txtSgender.getText().toString().trim();
  48. final String slocaltion = txtSlocation.getText().toString().trim();
  49. final String sphone = txtSphone.getText().toString().trim();
  50. final String semail = txtSemail.getText().toString().trim();
  51. final String cid = txtCid.getText().toString().trim();
  52. if (TextUtils.isEmpty(sid) || TextUtils.isEmpty(spassword) || TextUtils.isEmpty(sname) || TextUtils.isEmpty(sgender) || TextUtils.isEmpty(slocaltion) || TextUtils.isEmpty(sphone) || TextUtils.isEmpty(semail) || TextUtils.isEmpty(cid)) {
  53. CommonUtils.showShortMsg(this, "请输入完整信息");
  54. // txtSid.requestFocus();
  55. } else {
  56. new Thread(new Runnable() {
  57. @Override //重写run方法
  58. public void run() {
  59. item = new StudentInfo();
  60. item.setSid(sid);
  61. item.setSpassword(CommonUtils.getBcryptHash(spassword));
  62. item.setSname(sname);
  63. item.setSgender(sgender);
  64. item.setSlocation(slocaltion);
  65. item.setSphone(sphone);
  66. item.setSemail(semail);
  67. item.setCid(cid);
  68. studentDao.editStudent(item);
  69. mainHandler.post(new Runnable() {
  70. @Override
  71. public void run() {
  72. CommonUtils.showShortMsg(StudentEditActivity.this, "修改成功");
  73. // CommonUtils.showDialogMsg(MainActivity.this, "登录成功");
  74. setResult(1); //resultCode
  75. finish();
  76. // Intent intent = new Intent(StudentAddActivity.this, StudentManagerActivity.class);
  77. // startActivity(intent);
  78. }
  79. });
  80. }
  81. }).start(); //开启线程
  82. }
  83. }
  84. }

修改其样式文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".StudentAddActivity">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:focusable="true"
  13. android:focusableInTouchMode="true"
  14. android:orientation="horizontal">
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="学 号:"
  19. android:textColor="#00f"
  20. android:textSize="16sp"
  21. android:textStyle="bold"></TextView>
  22. <EditText
  23. android:id="@+id/txtSid"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:background="@null"
  27. android:hint="请输入学号"
  28. android:inputType="textPersonName"
  29. android:textColor="#00f"
  30. android:textSize="16sp"
  31. android:textStyle="bold"></EditText>
  32. </LinearLayout>
  33. <LinearLayout
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:orientation="horizontal">
  37. <TextView
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="密 码:"
  41. android:textColor="#00f"
  42. android:textSize="16sp"
  43. android:textStyle="bold"></TextView>
  44. <EditText
  45. android:id="@+id/txtSpassword"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:background="@null"
  49. android:hint="请输入密码"
  50. android:inputType="textPassword"
  51. android:textColor="#00f"
  52. android:textSize="16sp"
  53. android:textStyle="bold"></EditText>
  54. </LinearLayout>
  55. <LinearLayout
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"
  58. android:orientation="horizontal">
  59. <TextView
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="姓 名:"
  63. android:textColor="#00f"
  64. android:textSize="16sp"
  65. android:textStyle="bold"></TextView>
  66. <EditText
  67. android:id="@+id/txtSname"
  68. android:layout_width="match_parent"
  69. android:layout_height="wrap_content"
  70. android:background="@null"
  71. android:hint="请输入姓名"
  72. android:inputType="textPersonName"
  73. android:textColor="#00f"
  74. android:textSize="16sp"
  75. android:textStyle="bold"></EditText>
  76. </LinearLayout>
  77. <LinearLayout
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content"
  80. android:orientation="horizontal">
  81. <TextView
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:text="性 别:"
  85. android:textColor="#00f"
  86. android:textSize="16sp"
  87. android:textStyle="bold"></TextView>
  88. <EditText
  89. android:id="@+id/txtSgender"
  90. android:layout_width="match_parent"
  91. android:layout_height="wrap_content"
  92. android:background="@null"
  93. android:hint="请输入性别"
  94. android:inputType="textPersonName"
  95. android:textColor="#00f"
  96. android:textSize="16sp"
  97. android:textStyle="bold"></EditText>
  98. </LinearLayout>
  99. <LinearLayout
  100. android:layout_width="match_parent"
  101. android:layout_height="wrap_content"
  102. android:orientation="horizontal">
  103. <TextView
  104. android:layout_width="wrap_content"
  105. android:layout_height="wrap_content"
  106. android:text="地 址:"
  107. android:textColor="#00f"
  108. android:textSize="16sp"
  109. android:textStyle="bold"></TextView>
  110. <EditText
  111. android:id="@+id/txtSlocation"
  112. android:layout_width="match_parent"
  113. android:layout_height="wrap_content"
  114. android:background="@null"
  115. android:hint="请输入地址"
  116. android:inputType="textPostalAddress"
  117. android:textColor="#00f"
  118. android:textSize="16sp"
  119. android:textStyle="bold"></EditText>
  120. </LinearLayout>
  121. <LinearLayout
  122. android:layout_width="match_parent"
  123. android:layout_height="wrap_content"
  124. android:orientation="horizontal">
  125. <TextView
  126. android:layout_width="wrap_content"
  127. android:layout_height="wrap_content"
  128. android:text="手 机:"
  129. android:textColor="#00f"
  130. android:textSize="16sp"
  131. android:textStyle="bold"></TextView>
  132. <EditText
  133. android:id="@+id/txtSphone"
  134. android:layout_width="match_parent"
  135. android:layout_height="wrap_content"
  136. android:background="@null"
  137. android:hint="请输入手机号"
  138. android:inputType="phone"
  139. android:textColor="#00f"
  140. android:textSize="16sp"
  141. android:textStyle="bold"></EditText>
  142. </LinearLayout>
  143. <LinearLayout
  144. android:layout_width="match_parent"
  145. android:layout_height="wrap_content"
  146. android:orientation="horizontal">
  147. <TextView
  148. android:layout_width="wrap_content"
  149. android:layout_height="wrap_content"
  150. android:text="邮 箱:"
  151. android:textColor="#00f"
  152. android:textSize="16sp"
  153. android:textStyle="bold"></TextView>
  154. <EditText
  155. android:id="@+id/txtSemail"
  156. android:layout_width="match_parent"
  157. android:layout_height="wrap_content"
  158. android:background="@null"
  159. android:hint="请输入邮箱"
  160. android:inputType="textEmailAddress"
  161. android:textColor="#00f"
  162. android:textSize="16sp"
  163. android:textStyle="bold"></EditText>
  164. </LinearLayout>
  165. <LinearLayout
  166. android:layout_width="match_parent"
  167. android:layout_height="wrap_content"
  168. android:orientation="horizontal">
  169. <TextView
  170. android:layout_width="wrap_content"
  171. android:layout_height="wrap_content"
  172. android:text="班级id:"
  173. android:textColor="#00f"
  174. android:textSize="16sp"
  175. android:textStyle="bold"></TextView>
  176. <EditText
  177. android:id="@+id/txtCid"
  178. android:layout_width="match_parent"
  179. android:layout_height="wrap_content"
  180. android:background="@null"
  181. android:hint="请输入班级id"
  182. android:inputType="textPersonName"
  183. android:textColor="#00f"
  184. android:textSize="16sp"
  185. android:textStyle="bold"></EditText>
  186. </LinearLayout>
  187. <Button
  188. android:layout_width="match_parent"
  189. android:layout_height="wrap_content"
  190. android:onClick="btnEditClick"
  191. android:text="确 定"
  192. android:textColor="#0f0"
  193. android:textSize="18sp"
  194. android:textStyle="bold"></Button>
  195. </LinearLayout>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/279761
推荐阅读
相关标签
  

闽ICP备14008679号