体温一1
增加用户注册功能,用户注册信息包括用户ID(学号)、用户名(姓名),手机号码,用户单位(班级),用户班级四项基本信息
效果如下:
布局文件activity_zhuce.xml代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:padding="10dp" 8 tools:context=".ZhuceActivity"> 9 10 <TextView 11 android:id="@+id/zhuce_title" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:text="注册" 15 android:textSize="25sp" 16 android:gravity="center" 17 android:textColor="#000000" 18 /> 19 20 21 <EditText 22 android:id="@+id/zhuce_ID" 23 android:hint="学号" 24 android:textColor="#000000" 25 android:textSize="16sp" 26 android:layout_width="match_parent" 27 android:layout_height="50dp" 28 android:padding="5dp" 29 android:digits="1234567890." 30 android:layout_below="@id/zhuce_title" 31 android:layout_marginTop="20dp" 32 android:background="#FDDEDE" 33 /> 34 35 <EditText 36 android:id="@+id/zhuce_name" 37 android:hint="姓名" 38 android:textColor="#000000" 39 android:textSize="16sp" 40 android:layout_width="match_parent" 41 android:layout_height="50dp" 42 android:padding="5dp" 43 android:layout_below="@id/zhuce_ID" 44 android:layout_marginTop="20dp" 45 android:background="#FDDEDE" 46 /> 47 48 <EditText 49 android:id="@+id/zhuce_phone" 50 android:layout_width="match_parent" 51 android:layout_height="50dp" 52 android:layout_below="@id/zhuce_name" 53 android:layout_marginTop="20dp" 54 android:background="#FDDEDE" 55 android:hint="手机号码" 56 android:digits="1234567890" 57 android:padding="5dp" 58 android:textColor="#000000" 59 android:textSize="16sp" /> 60 61 <EditText 62 android:id="@+id/zhuce_class" 63 android:hint="班级" 64 android:textColor="#000000" 65 android:textSize="16sp" 66 android:layout_width="match_parent" 67 android:layout_height="50dp" 68 android:padding="5dp" 69 android:layout_below="@id/zhuce_phone" 70 android:layout_marginTop="20dp" 71 android:background="#FDDEDE" 72 android:clickable="true" 73 /> 74 75 <Button 76 android:id="@+id/btn_zc" 77 android:layout_width="match_parent" 78 android:layout_height="wrap_content" 79 android:text="注册" 80 android:layout_below="@id/zhuce_class" 81 android:layout_marginTop="20dp" 82 android:background="#1E86FD" 83 /> 84 85 </RelativeLayout>
ZhuceActivity.java
1 public class ZhuceActivity extends AppCompatActivity { 2 //注册按钮 3 private Button mbtn_zhuce; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_zhuce); 8 9 //注册按钮 10 mbtn_zhuce = findViewById(R.id.btn_zc); 11 mbtn_zhuce.setOnClickListener(new View.OnClickListener() { 12 @Override 13 public void onClick(View v) { 14 final EditText IDET=findViewById(R.id.zhuce_ID);//获取ID 15 final EditText NameET=findViewById(R.id.zhuce_name);//获取姓名 16 final EditText PhoneET=findViewById(R.id.zhuce_phone);//获取电话 17 final EditText ClassET=findViewById(R.id.zhuce_class);//获取班级 18 19 //存数据 20 mbtn_zhuce.setOnClickListener(new View.OnClickListener() { 21 @Override 22 public void onClick(View v) { 23 Toast.makeText(ZhuceActivity.this,"注册成功",Toast.LENGTH_SHORT).show(); 24 Intent intent=null; 25 intent=new Intent(ZhuceActivity.this,MainActivity.class); 26 startActivity(intent); 27 } 28 }); 29 } 30 }); 31 } 32 }
MyDatabaseHelper.java
1 import android.content.Context; 2 import android.database.sqlite.SQLiteDatabase; 3 import android.database.sqlite.SQLiteOpenHelper; 4 5 public class MyDatabaseHelper extends SQLiteOpenHelper { 6 7 private Context mContext; 8 public MyDatabaseHelper(Context context){ 9 super(context,Constants.DATABASE_NAME,null,Constants.VERSION_CODE); 10 mContext=context; 11 } 12 13 @Override 14 public void onCreate(SQLiteDatabase db){ 15 //创建时的回调 16 String sqlt="create table " + Constants.User_NAME + "(userID varchar,userName varchar,userPhone varchar,userClass varchar)"; 17 db.execSQL(sqlt); 18 } 19 20 @Override 21 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ 22 //升级时的回调 23 } 24 25 }
Constants.java
1 public class Constants { 2 public static final String DATABASE_NAME ="TemperatureAPP.db"; 3 public static final int VERSION_CODE=1; 4 public static final String User_NAME="user"; 5 }
User.java
1 public class User { 2 3 private String userID; 4 private String userName; 5 private String userPhone; 6 private String userClass; 7 8 public String getUserID() { 9 return userID; 10 } 11 12 public void setUserID(String userID) { 13 this.userID = userID; 14 } 15 16 public String getUserClass() { 17 return userClass; 18 } 19 20 public void setUserClass(String userClass) { 21 this.userClass = userClass; 22 } 23 24 public String getUserPhone() { 25 return userPhone; 26 } 27 28 public void setUserPhone(String userPhone) { 29 this.userPhone = userPhone; 30 } 31 32 public String getUserName() { 33 return userName; 34 } 35 36 public void setUserName(String userName) { 37 this.userName = userName; 38 } 39 40 public User(){} 41 42 public User(String userID,String userClass,String userPhone,String userName){ 43 super(); 44 this.userClass=userClass; 45 this.userPhone=userPhone; 46 this.userName=userName; 47 this.userID=userID; 48 } 49 50 @Override 51 public String toString() { 52 return "User{" + "userID='" + userID + '\'' + ", userName='" + userName + '\''+ ", userPhone='" + userPhone + '\''+ ", userClass='" +userClass + '\''+ '}'; 53 } 54 }
Dao.java
1 import android.content.Context; 2 import android.database.Cursor; 3 import android.database.sqlite.SQLiteDatabase; 4 5 public class Dao { 6 7 private final MyDatabaseHelper mHelper; 8 9 public Dao(Context context){ 10 //创建数据库 11 mHelper=new MyDatabaseHelper(context); 12 } 13 14 public void insertUser(String userID,String userName,String userPhone,String userClass){ 15 SQLiteDatabase db=mHelper.getWritableDatabase(); 16 String sql="insert into "+Constants.User_NAME+"(userID,userName,userPhone,userClass) values(?,?,?,?)"; 17 db.execSQL(sql,new Object[]{userID,userName,userPhone,userClass}); 18 db.close(); 19 } 20 }