赞
踩
目录
来得晚的小伙伴们请先看上集:(35条消息) Android Studio实现Mysql(5.7)数据库增删改查(上)——用户登录功能实现_TidesWise的博客-CSDN博客
1、在libs文件夹下添加jbcrypt-0.4.jar包,具体资源自行互联网下载,再右键add as Library,具体操作参见上集;
2、在CommonUtils添加以下两个方法,用以得到哈希加密的密码和验证用户输入密码是否与数据库中加密密码一致;注意引入jbcrypt:
- import org.mindrot.jbcrypt.BCrypt;
-
- public static String getBcryptHash(String spassword) {
- String encodePwd = BCrypt.hashpw(spassword, BCrypt.gensalt()); // 加密,核心代码
- return encodePwd;
- }
- public static boolean checkPw(String spassword, String hashedPw) {
- boolean flag = BCrypt.checkpw(spassword, hashedPw); // 验证加密是否正确
- return flag;
- }
3、淘汰StudentDao.java类中的getUserByLogin方法,新增getUserBySid方法;
- public StudentInfo getUserBySid(String sid) {
- StudentInfo item = null;
- try {
- getConnection();
- String sql = "select * from student where sid=?";
- pStmt = conn.prepareStatement(sql);
- pStmt.setString(1, sid);
- rs = pStmt.executeQuery();
- if (rs.next()) {
- item = new StudentInfo();
- item.setSid(sid);
- item.setSpassword(rs.getString("spassword"));
- item.setSname(rs.getString("sname"));
- item.setSgender(rs.getString("gender"));
- item.setSlocation(rs.getString("slocation"));
- item.setSphone(rs.getString("sphone"));
- item.setSemail(rs.getString("semail"));
- item.setCid(rs.getString("cid"));
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- closeAll();
- }
- return item;
- }
4、自此在登陆操作时,我们只需通过getUserBySid方法获取数据库中加密的密码,再通过CommonUtils类中方法checkPw比较决定用户是否登陆操作成功;修改MainActivity.java中的doLogin方法如下所示:
- private void doLogin() {
- final String sid = txtSid.getText().toString().trim();
- final String spassword = txtSpassword.getText().toString().trim();
- if (TextUtils.isEmpty(sid)) {
- CommonUtils.showShortMsg(this, "请输入用户名");
- txtSid.requestFocus();
- } else if (TextUtils.isEmpty(spassword)) {
- CommonUtils.showShortMsg(this, "请输入密码");
- txtSpassword.requestFocus();
- } else {
- new Thread(new Runnable() {
- @Override //重写run方法
- public void run() {
- final StudentInfo item;
- if (dao.getUserBySid(sid) == null) //检验是否有此用户账号
- item = null;
- else if (CommonUtils.checkPw(spassword, dao.getUserBySid(sid).getSpassword())) //检验用户输入密码是否与数据库存储的加密密码一致
- item = dao.getUserBySid(sid);
- else item = null; //不一致的情况
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- if (item == null)
- CommonUtils.showDialogMsg(MainActivity.this, "用户名或密码错误");
- else
- CommonUtils.showDialogMsg(MainActivity.this, "登录成功");
- }
- });
- }
- }).start(); //开启线程
- }
- }
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:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:orientation="vertical">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/txtSindex"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="1."
- android:textColor="#9C27B0"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <TextView
- android:id="@+id/txtSid"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:text="202021136021"
- android:textColor="#9C27B0"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <TextView
- android:id="@+id/txtSname"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:text="XXX"
- android:textColor="#9C27B0"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:gravity="right">
-
- <ImageView
- android:id="@+id/btnEdit"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginRight="10dp"
- android:src="@drawable/edit"></ImageView>
-
- <ImageView
- android:id="@+id/btnDelete"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginRight="10dp"
- android:src="@drawable/delete"></ImageView>
- </LinearLayout>
-
- </LinearLayout>
- </LinearLayout>
3、创建接口OnEditBtnClickListener和OnDeleteBtnClickListener :
- package com.example.myapplication;
-
- import android.view.View;
-
- /*
- * 修改按钮点击事件监听接口
- */
- public interface OnEditBtnClickListener {
- void onEditBtnClick(View view, int position);
- }
- package com.example.myapplication;
- /*删除按钮点击事件*/
-
- import android.view.View;
-
- public interface OnDeleteBtnClickListener {
- void onDeleteBtnClick(View view, int position);
- }
4、创建自定义用户适配器类LvStudentInfoAdapter :
- package com.example.myapplication;
- /*自定义用户适配器类*/
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- import java.util.List;
-
- public class LvStudentInfoAdapter extends BaseAdapter {
- private Context context;
- private List<StudentInfo> studentInfoList;
- private OnEditBtnClickListener onEditBtnClickListener;
- private OnDeleteBtnClickListener onDeleteBtnClickListener;
-
- public LvStudentInfoAdapter() {
- }
-
- public LvStudentInfoAdapter(Context context, List<StudentInfo> studentInfoList) {
- this.context = context;
- this.studentInfoList = studentInfoList;
- }
-
- public void setStudentInfoList(List<StudentInfo> studentInfoList) {
- this.studentInfoList = studentInfoList;
- }
-
- public void setOnEditBtnClickListener(OnEditBtnClickListener onEditBtnClickListener) {
- this.onEditBtnClickListener = onEditBtnClickListener;
- }
-
- public void setOnDeleteBtnClickListener(OnDeleteBtnClickListener onDeleteBtnClickListener) {
- this.onDeleteBtnClickListener = onDeleteBtnClickListener;
- }
-
- @Override
- public int getCount() {
- return studentInfoList.size();
- }
-
- @Override
- public Object getItem(int i) {
- return studentInfoList.get(i);
- }
-
- @Override
- public long getItemId(int i) {
- return i;
- }
-
- @Override
- public View getView(int i, View convertView, ViewGroup viewGroup) {
- // return null;
- ViewHolder viewHolder = null;
- if (convertView == null) {
- convertView = LayoutInflater.from(context).inflate(R.layout.student_list_item, null);
- viewHolder = new ViewHolder();
- viewHolder.txtSid = convertView.findViewById(R.id.txtSid);
- viewHolder.txtSindex = convertView.findViewById(R.id.txtSindex);
- viewHolder.txtSname = convertView.findViewById(R.id.txtSname);
- viewHolder.btnEdit = convertView.findViewById(R.id.btnEdit);
- viewHolder.btnDelete = convertView.findViewById(R.id.btnDelete);
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
- StudentInfo item = studentInfoList.get(i);
- viewHolder.txtSindex.setText(i + 1 + ".");
- viewHolder.txtSid.setText(item.getSid());
- viewHolder.txtSname.setText(item.getSname());
- viewHolder.btnEdit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- onEditBtnClickListener.onEditBtnClick(view, i);
- }
- });
- viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- onDeleteBtnClickListener.onDeleteBtnClick(view, i);
- }
- });
- return convertView;
- }
-
- //自定义内部类
- private class ViewHolder {
- private TextView txtSindex, txtSid, txtSname;
- private ImageView btnEdit, btnDelete;
- }
- }
5、创建StudentManagerActivity:
- package com.example.myapplication;
-
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.ListView;
-
- import java.util.List;
-
- public class StudentManagerActivity extends AppCompatActivity implements View.OnClickListener {
- private ImageView btnReturn, btnAdd;
- private ListView lvStudent;
- private Handler mainHandler;
- private StudentDao studentDao; //用户数据库操作实例
- private List<StudentInfo> studentInfoList;
- private LvStudentInfoAdapter lvStudentInfoAdapter; //用户信息数据适配器
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_student_manager);
- initView();
- loadStudentList();
- }
-
- private void initView() {
- studentDao = new StudentDao();
- btnReturn = findViewById(R.id.btnReturn);
- btnAdd = findViewById(R.id.btnAdd);
- lvStudent = findViewById(R.id.lvStudent);
- mainHandler = new Handler(getMainLooper());
- btnReturn.setOnClickListener(this);
- btnAdd.setOnClickListener(this);
- }
-
- private void loadStudentList() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- studentInfoList = studentDao.getStudentList();
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- showLvData();
- }
- });
- }
- }).start();
- }
-
- private void showLvData() {
- if (lvStudentInfoAdapter == null) {
- lvStudentInfoAdapter = new LvStudentInfoAdapter(this, studentInfoList);
- lvStudent.setAdapter(lvStudentInfoAdapter);
- } else {
- lvStudentInfoAdapter.setStudentInfoList(studentInfoList);
- lvStudentInfoAdapter.notifyDataSetChanged();
- }
- //修改按钮
- lvStudentInfoAdapter.setOnEditBtnClickListener(new OnEditBtnClickListener() {
- @Override
- public void onEditBtnClick(View view, int position) {
- StudentInfo item = studentInfoList.get(position);
- Bundle bundle = new Bundle();
- bundle.putSerializable("item", item);
- Intent intent = new Intent(StudentManagerActivity.this, StudentEditActivity.class);
- intent.putExtras(bundle);
- startActivityForResult(intent, 1);
- }
- });
- //删除按钮
- lvStudentInfoAdapter.setOnDeleteBtnClickListener(new OnDeleteBtnClickListener() {
- @Override
- public void onDeleteBtnClick(View view, int position) {
- final String sid = studentInfoList.get(position).getSid();
- AlertDialog.Builder builder = new AlertDialog.Builder(StudentManagerActivity.this);
- builder.setTitle("提示信息");
- builder.setMessage("您确定要删除学生用户" + studentInfoList.get(position).getSname() + "的信息吗?");
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- studentDao.delStudent(sid);
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- loadStudentList();
- }
- });
- }
- }).start();
- }
- });
- builder.setNegativeButton("取消", null);
- builder.create().show();
-
- }
- });
- }
-
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.btnReturn:
- finish(); //结束当前页面
- break;
- case R.id.btnAdd:
- Intent intent = new Intent(this, StudentAddActivity.class);
- startActivityForResult(intent, 1);//requestCode
- break;
- }
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 1 && resultCode == 1) {
- loadStudentList();
- }
- }
-
- }
修改其样式文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="5dp"
-
- android:orientation="vertical"
- tools:context=".StudentManagerActivity">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
-
- <ImageView
- android:id="@+id/btnReturn"
- android:layout_width="40dp"
- android:layout_height="40dp"
- android:src="@drawable/goback" />
-
- <ImageView
- android:id="@+id/btnAdd"
- android:layout_width="40dp"
- android:layout_height="40dp"
- android:layout_alignParentRight="true"
- android:src="@drawable/add" />
- </LinearLayout>
-
- <ListView
- android:id="@+id/lvStudent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingTop="0dp" />
-
- </LinearLayout>
- </LinearLayout>
6、创建StudentAddActivity:
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.EditText;
-
- public class StudentAddActivity extends AppCompatActivity {
- private EditText txtSid, txtSpassword, txtSname, txtSgender, txtSlocation, txtSphone, txtSemail, txtCid;
- private Handler mainHandler;
- private StudentDao studentDao;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_student_add);
- initView();
- }
-
- private void initView() {
- txtSid = findViewById(R.id.txtSid);
- txtSpassword = findViewById(R.id.txtSpassword);
- txtSname = findViewById(R.id.txtSname);
- txtSgender = findViewById(R.id.txtSgender);
- txtSlocation = findViewById(R.id.txtSlocation);
- txtSphone = findViewById(R.id.txtSphone);
- txtSemail = findViewById(R.id.txtSemail);
- txtCid = findViewById(R.id.txtCid);
- mainHandler = new Handler(getMainLooper());
- studentDao = new StudentDao();
- }
-
- public void btnAddClick(View view) {
- final String sid = txtSid.getText().toString().trim();
- final String spassword = txtSpassword.getText().toString().trim();
- final String sname = txtSname.getText().toString().trim();
- final String sgender = txtSgender.getText().toString().trim();
- final String slocaltion = txtSlocation.getText().toString().trim();
- final String sphone = txtSphone.getText().toString().trim();
- final String semail = txtSemail.getText().toString().trim();
- final String cid = txtCid.getText().toString().trim();
-
- 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)) {
- CommonUtils.showShortMsg(this, "请输入完整信息");
- // txtSid.requestFocus();
- } else {
- new Thread(new Runnable() {
- @Override //重写run方法
- public void run() {
- final StudentInfo item;
- if (studentDao.getStudentBySid(sid) != null) //检验是否有此用户账号
- item = null;
- else {
- item = new StudentInfo();
- item.setSid(sid);
- item.setSpassword(spassword);
- item.setSname(sname);
- item.setSgender(sgender);
- item.setSlocation(slocaltion);
- item.setSphone(sphone);
- item.setSemail(semail);
- item.setCid(cid);
- studentDao.addStudent(item);
- }
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- if (item == null)
- CommonUtils.showShortMsg(StudentAddActivity.this, "学号已存在");
- else {
- CommonUtils.showShortMsg(StudentAddActivity.this, "添加成功");
- // CommonUtils.showDialogMsg(MainActivity.this, "登录成功");
- setResult(1); //resultCode
- finish();
- // Intent intent = new Intent(StudentAddActivity.this, StudentManagerActivity.class);
- // startActivity(intent);
- }
- }
- });
- }
- }).start(); //开启线程
- }
- }
- }
修改其样式文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".StudentAddActivity">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="学 号:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSid"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入学号"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密 码:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSpassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入密码"
- android:inputType="textPassword"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="姓 名:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSname"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入姓名"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="性 别:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSgender"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入性别"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="地 址:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSlocation"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入地址"
- android:inputType="textPostalAddress"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="手 机:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSphone"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入手机号"
- android:inputType="phone"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="邮 箱:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSemail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入邮箱"
- android:inputType="textEmailAddress"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="班级id:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtCid"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入班级id"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="btnAddClick"
- android:text="确 定"
- android:textColor="#0f0"
- android:textSize="18sp"
- android:textStyle="bold"></Button>
- </LinearLayout>
7、创建StudentEditActivity:
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.os.Handler;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.EditText;
-
- public class StudentEditActivity extends AppCompatActivity {
- private EditText txtSid, txtSpassword, txtSname, txtSgender, txtSlocation, txtSphone, txtSemail, txtCid;
- private Handler mainHandler;
- private StudentDao studentDao;
- private StudentInfo item;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_student_edit);
- initView();
- }
-
- private void initView() {
- Bundle bundle = getIntent().getExtras();
- txtSid = findViewById(R.id.txtSid);
- txtSpassword = findViewById(R.id.txtSpassword);
- txtSname = findViewById(R.id.txtSname);
- txtSgender = findViewById(R.id.txtSgender);
- txtSlocation = findViewById(R.id.txtSlocation);
- txtSphone = findViewById(R.id.txtSphone);
- txtSemail = findViewById(R.id.txtSemail);
- txtCid = findViewById(R.id.txtCid);
- if (bundle != null) {
- item = (StudentInfo) bundle.getSerializable("item");
- txtSid.setText(item.getSid());
- // txtSpassword.setText(item.getSpassword());
- txtSname.setText(item.getSname());
- txtSgender.setText(item.getSgender());
- txtSlocation.setText(item.getSlocation());
- txtSphone.setText(item.getSphone());
- txtSemail.setText(item.getSemail());
- txtCid.setText(item.getCid());
- }
- mainHandler = new Handler(getMainLooper());
- studentDao = new StudentDao();
- }
-
- public void btnEditClick(View view) {
- final String sid = txtSid.getText().toString().trim();
- final String spassword = txtSpassword.getText().toString().trim();
- final String sname = txtSname.getText().toString().trim();
- final String sgender = txtSgender.getText().toString().trim();
- final String slocaltion = txtSlocation.getText().toString().trim();
- final String sphone = txtSphone.getText().toString().trim();
- final String semail = txtSemail.getText().toString().trim();
- final String cid = txtCid.getText().toString().trim();
-
- 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)) {
- CommonUtils.showShortMsg(this, "请输入完整信息");
- // txtSid.requestFocus();
- } else {
- new Thread(new Runnable() {
- @Override //重写run方法
- public void run() {
- item = new StudentInfo();
- item.setSid(sid);
- item.setSpassword(CommonUtils.getBcryptHash(spassword));
- item.setSname(sname);
- item.setSgender(sgender);
- item.setSlocation(slocaltion);
- item.setSphone(sphone);
- item.setSemail(semail);
- item.setCid(cid);
- studentDao.editStudent(item);
-
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- CommonUtils.showShortMsg(StudentEditActivity.this, "修改成功");
- // CommonUtils.showDialogMsg(MainActivity.this, "登录成功");
- setResult(1); //resultCode
- finish();
- // Intent intent = new Intent(StudentAddActivity.this, StudentManagerActivity.class);
- // startActivity(intent);
- }
- });
- }
- }).start(); //开启线程
- }
- }
- }
修改其样式文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".StudentAddActivity">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="学 号:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSid"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入学号"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密 码:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSpassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入密码"
- android:inputType="textPassword"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="姓 名:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSname"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入姓名"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="性 别:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSgender"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入性别"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="地 址:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSlocation"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入地址"
- android:inputType="textPostalAddress"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="手 机:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSphone"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入手机号"
- android:inputType="phone"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="邮 箱:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtSemail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入邮箱"
- android:inputType="textEmailAddress"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="班级id:"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></TextView>
-
- <EditText
- android:id="@+id/txtCid"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@null"
- android:hint="请输入班级id"
- android:inputType="textPersonName"
- android:textColor="#00f"
- android:textSize="16sp"
- android:textStyle="bold"></EditText>
- </LinearLayout>
-
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="btnEditClick"
- android:text="确 定"
- android:textColor="#0f0"
- android:textSize="18sp"
- android:textStyle="bold"></Button>
- </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。