Loading

Android-每日打卡APP-实现用户注册

今天晚上接着写了会打卡APP-现在实现了用户注册,我打算一步步来,一个一个功能实现,这也是一个比较谨慎和比较好的习惯

虽然之前已经实现过添加数据了,但毕竟参考的比较多,这次拿新项目练手,就再写一遍,让自己更加熟练一些

这部分的代码也会放在后边

先来看一下效果,进入app,点击注册

 进入注册页面,写入自己的信息

 

 跳出注册成功字样

 找到保存并打开数据库文件,在Android中的操作如图

 找到数据库文件crud.db

具体路径是data/data/com.(你的包名)/databases/crud.db

 我们保存下来再用SQLite Expert Professional 3打开

 可以看到我们刚刚插入的数据,还有之前我插入过的数据

插入成功!

代码:

DB.java

package com.example.clockappliction.DataBase;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.clockappliction.Information.Clock;
import com.example.clockappliction.Information.Student;

public class DB extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION=1;

    private static final String DATABASE_NAME="crud.db";

    public DB(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_TABLE_USER="CREATE TABLE IF NOT EXISTS " + Student.TABLE +"("
                +Student.KEYWORD+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
                +Student.ID+" TEXT ,"
                +Student.NAME+" TEXT ,"
                +Student.GRADE+" TEXT ,"
                +Student.PHONE+" TEXT)";

        db.execSQL(CREATE_TABLE_USER);

        String CREATE_TABLE_CLOCK="CREATE TABLE IF NOT EXISTS " + Clock.TABLE +"("
                +Clock.KEYWORD+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
                +Clock.DATE+" TEXT ,"
                +Clock.WORD+" TEXT ,"
                +Clock.SUMMARY+" TEXT ,"
                +Clock.MAXDAY+" TEXT)";

        db.execSQL(CREATE_TABLE_CLOCK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS "+ Student.TABLE);
        db.execSQL("DROP TABLE IF EXISTS "+ Clock.TABLE);
        onCreate(db);
    }
}

CRUD.java

package com.example.clockappliction.DataBase;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.clockappliction.DataBase.DB;
import com.example.clockappliction.Information.Student;

public class CRUD {

    private DB db;

    public CRUD (Context context){db = new DB(context);}

    public int insertStudent(Student student){

        SQLiteDatabase sd = db.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(Student.ID,student.id);
        values.put(Student.NAME,student.name);
        values.put(Student.GRADE,student.grade);
        values.put(Student.PHONE,student.phone);

        long keywords = sd.insert(Student.TABLE,null,values);
        sd.close();
        return (int) keywords;
    }

    @SuppressLint("Range")
    public Student getStudentByKeyWord(int keyword){

        SQLiteDatabase sd = db.getReadableDatabase();
        String selectQuery = "SELECT "+
                Student.KEYWORD +","+
                Student.ID +","+
                Student.NAME +","+
                Student.GRADE + ","+
                Student.PHONE+
                " FROM " + Student.TABLE
                +" WHERE " +
                Student.KEYWORD + "=?";
        int count = 0;
        Student student = new Student();
        Cursor cursor = sd.rawQuery(selectQuery,new String[]{String.valueOf(keyword)});
        if (cursor.moveToFirst()){
            do {
                student.keyword = cursor.getInt(cursor.getColumnIndex(Student.KEYWORD));
                student.id = cursor.getString(cursor.getColumnIndex(Student.ID));
                student.name = cursor.getString(cursor.getColumnIndex(Student.NAME));
                student.grade = cursor.getString(cursor.getColumnIndex(Student.GRADE));
                student.phone = cursor.getString(cursor.getColumnIndex(Student.PHONE));
            }while (cursor.moveToNext());
        }
        cursor.close();
        sd.close();
        return student;
    }

}

Student.java

package com.example.clockappliction.Information;

public class Student {

    public int keyword;

    public String id;
    public String name;
    public String grade;
    public String phone;

    public static final String TABLE = "Student_Info";

    public static final String KEYWORD = "keyword";

    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String GRADE = "grade";
    public static final String PHONE = "phone";
}

MainActivity.java

package com.example.clockappliction;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_login, btn_reg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);

        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        if (view == findViewById(R.id.btn_login)){
            Intent intent = new Intent(this, MenuActivity.class);
            startActivity(intent);
        } else if (view == findViewById(R.id.btn_reg)) {
            Intent intent = new Intent(this,RegActivity.class);
            intent.putExtra("keys",0);
            startActivity(intent);
        }
    }

}

RegActivity.java

package com.example.clockappliction;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.clockappliction.DataBase.CRUD;
import com.example.clockappliction.Information.Student;

public class RegActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_reg_add;

    private EditText et_reg_id,et_reg_name,et_reg_grade,et_reg_phone;
    private int keys  = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        btn_reg_add = (Button) findViewById(R.id.btn_reg_add);
        btn_reg_add.setOnClickListener(this);

        et_reg_id = (EditText) findViewById(R.id.et_reg_id);
        et_reg_name = (EditText) findViewById(R.id.et_reg_name);
        et_reg_grade = (EditText) findViewById(R.id.et_reg_grade);
        et_reg_phone = (EditText) findViewById(R.id.et_reg_phone);

        Intent intent = getIntent();
        keys = intent.getIntExtra("keys",0);
        CRUD crud = new CRUD(this);
        Student student = new Student();
        student = crud.getStudentByKeyWord(keys);

        et_reg_id.setText(student.id);
        et_reg_name.setText(student.name);
        et_reg_grade.setText(student.grade);
        et_reg_phone.setText(student.phone);
    }

    @Override
    public void onClick(View view) {
        if (view == findViewById(R.id.btn_reg_add)){

            CRUD crud = new CRUD(this);
            Student student = new Student();

            student.keyword = keys;
            student.id = et_reg_id.getText().toString();
            student.name = et_reg_name.getText().toString();
            student.grade = et_reg_grade.getText().toString();
            student.phone = et_reg_phone.getText().toString();

            keys = crud.insertStudent(student);

            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        }
    }
}

actitvty_register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:background="#DDDDDD"
    tools:context=".RegActivity">

    <TextView
        android:id="@+id/tv_title2"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="98dp"
        android:layout_marginTop="98dp"
        android:text="注册账号"
        android:textColor="#000"
        android:textSize="50sp"
        android:textStyle="bold" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_reg_id"
        android:hint="学号"

        android:layout_below="@+id/tv_title2"
        android:layout_marginTop="15dp"
        ></EditText>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_reg_name"
        android:hint="姓名"

        android:layout_below="@+id/et_reg_id"
        android:layout_marginTop="10dp"
        ></EditText>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_reg_grade"
        android:hint="班级"

        android:layout_below="@+id/et_reg_name"
        android:layout_marginTop="10dp"
        ></EditText>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_reg_phone"
        android:hint="电话号码"

        android:layout_below="@+id/et_reg_grade"
        android:layout_marginTop="10dp"
        ></EditText>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/btn_reg_add"
        android:text="点击注册"

        android:textSize="30dp"
        android:layout_below="@+id/et_reg_phone"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="120dp"

        ></Button>

</RelativeLayout>

 

posted @ 2023-03-04 00:39  冰稀饭Aurora  阅读(46)  评论(0编辑  收藏  举报