2024.3.28

第十八天

所花时间:2小时

代码量:400+

博客量:1

了解到的知识点:个人学习记录app——登陆注册2

  1 package com.hui.study;
  2 
  3 import androidx.appcompat.app.AppCompatActivity;
  4 import android.annotation.SuppressLint;
  5 import android.content.Intent;
  6 import android.os.Bundle;
  7 import android.os.Handler;
  8 import android.os.Message;
  9 import android.util.Log;
 10 import android.view.View;
 11 import android.widget.EditText;
 12 import android.widget.Toast;
 13 
 14 import com.hui.study.dao.StudentDao;
 15 import com.hui.study.entity.Student;
 16 
 17 /**
 18  * function:连接注册页面
 19  */
 20 public class RegisterActivity extends AppCompatActivity {
 21     private static final String TAG = "mysql-party-register";
 22     EditText id = null;
 23 
 24     EditText name = null;
 25     EditText phone = null;
 26     EditText classroom = null;
 27 
 28 //    @SuppressLint("MissingInflatedId")
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_register);
 33 
 34         id = findViewById(R.id.studentid);
 35         name = findViewById(R.id.studentName);
 36         phone = findViewById(R.id.phone);
 37         classroom = findViewById(R.id.classroom);
 38     }
 39 
 40 
 41     public void register(View view){
 42 
 43         String id1 = id.getText().toString();
 44         String name1 = name.getText().toString();
 45         String phone1 = phone.getText().toString();
 46         String classroom1 = classroom.getText().toString();
 47 
 48 
 49         Student student = new Student();
 50 
 51         student.setId(id1);
 52         student.setName(name1);
 53         student.setPhone(phone1);
 54         student.setClassroom(classroom1);
 55 
 56         new Thread(){
 57             @Override
 58             public void run() {
 59 
 60                 int msg = 0;
 61 
 62                 StudentDao studentDao = new StudentDao();
 63 
 64                 Student uu = studentDao.findStudent(student.getId());
 65                 if(uu != null){
 66                     msg = 1;
 67                 }
 68                 else{
 69                     boolean flag = studentDao.register(student);
 70                     if(flag){
 71                         msg = 2;
 72                     }
 73                 }
 74                 hand.sendEmptyMessage(msg);
 75 
 76             }
 77         }.start();
 78 
 79 
 80     }
 81     @SuppressLint("HandlerLeak")
 82     final Handler hand = new Handler()
 83     {
 84         public void handleMessage(Message msg) {
 85             if(msg.what == 0) {
 86                 Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
 87             } else if(msg.what == 1) {
 88                 Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();
 89             } else if(msg.what == 2) {
 90                 Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
 91                 Intent intent = new Intent();
 92                 //将想要传递的数据用putExtra封装在intent中
 93                 intent.putExtra("a","注册");
 94                 setResult(RESULT_CANCELED,intent);
 95                 startActivity(new Intent(getApplicationContext(),MainActivity.class));
 96                 finish();
 97             }
 98         }
 99     };
100 }
  1 <?xml version="1.0" encoding="utf-8"?>
  2 <androidx.constraintlayout.widget.ConstraintLayout 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     tools:context=".RegisterActivity">
  8 
  9     <LinearLayout
 10         android:layout_width="match_parent"
 11         android:layout_height="match_parent"
 12         android:orientation="vertical"
 13         tools:layout_editor_absoluteX="219dp"
 14         tools:layout_editor_absoluteY="207dp"
 15         android:padding="50dp"
 16 
 17         >
 18 
 19         <LinearLayout
 20             android:layout_width="match_parent"
 21             android:layout_height="wrap_content"
 22             android:orientation="horizontal">
 23 
 24 
 25             <TextView
 26                 android:id="@+id/textView"
 27                 android:layout_width="wrap_content"
 28                 android:layout_height="wrap_content"
 29                 android:layout_weight="1"
 30                 android:textSize="15sp"
 31                 android:text="用户ID(学号):" />
 32 
 33             <EditText
 34                 android:id="@+id/studentid"
 35                 android:layout_width="wrap_content"
 36                 android:layout_height="wrap_content"
 37                 android:layout_weight="1"
 38                 android:ems="10"
 39                 android:inputType="phone"
 40                 />
 41         </LinearLayout>
 42         <LinearLayout
 43             android:layout_width="match_parent"
 44             android:layout_height="wrap_content"
 45             android:orientation="horizontal">
 46 
 47 
 48             <TextView
 49                 android:layout_width="wrap_content"
 50                 android:layout_height="wrap_content"
 51                 android:layout_weight="1"
 52                 android:textSize="15sp"
 53                 android:text="用户姓名:" />
 54 
 55             <EditText
 56                 android:id="@+id/studentName"
 57                 android:layout_width="wrap_content"
 58                 android:layout_height="wrap_content"
 59                 android:layout_weight="1"
 60                 android:ems="10"
 61                 android:inputType="textPersonName"
 62                 />
 63         </LinearLayout>
 64         <LinearLayout
 65             android:layout_width="match_parent"
 66             android:layout_height="wrap_content"
 67             android:orientation="horizontal">
 68 
 69 
 70             <TextView
 71                 android:id="@+id/textView2"
 72                 android:layout_width="wrap_content"
 73                 android:layout_height="wrap_content"
 74                 android:layout_weight="1"
 75 
 76                 android:textSize="15sp"
 77                 android:text="电话:"
 78 
 79                 />
 80 
 81             <EditText
 82                 android:id="@+id/phone"
 83                 android:layout_width="wrap_content"
 84                 android:layout_height="wrap_content"
 85                 android:layout_weight="1"
 86                 android:ems="10"
 87                 android:inputType="textVisiblePassword"
 88                 />
 89         </LinearLayout>
 90         <LinearLayout
 91             android:layout_width="match_parent"
 92             android:layout_height="wrap_content"
 93             android:orientation="horizontal">
 94 
 95 
 96             <TextView
 97                 android:id="@+id/textView3"
 98                 android:layout_width="wrap_content"
 99                 android:layout_height="wrap_content"
100                 android:layout_weight="1"
101 
102                 android:textSize="15sp"
103                 android:text="班级:"
104 
105                 />
106 
107             <EditText
108                 android:id="@+id/classroom"
109                 android:layout_width="wrap_content"
110                 android:layout_height="wrap_content"
111                 android:layout_weight="1"
112                 android:ems="10"
113                 android:inputType="text"
114                 />
115         </LinearLayout>
116         <LinearLayout
117             android:layout_width="match_parent"
118             android:layout_height="wrap_content"
119             android:orientation="horizontal">
120 
121         </LinearLayout>
122 
123         <Button
124             android:layout_marginTop="50dp"
125             android:id="@+id/button2"
126             android:layout_width="match_parent"
127             android:layout_height="wrap_content"
128             android:text="注册"
129             android:onClick="register"
130             />
131     </LinearLayout>
132 </androidx.constraintlayout.widget.ConstraintLayout>

 

posted @ 2024-04-07 22:30  cvjj  阅读(22)  评论(0)    收藏  举报