每日总结 3.2

今天学习了注册界面和数据库的链接。

<?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="match_parent"
    android:orientation="vertical"
    android:background="@color/grey7d">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="用户名(学号):"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="25sp"
            android:layout_marginTop="30dp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userpassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:hint="请输入密码"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"
            android:inputType="textWebPassword"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userNames"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="手机号:"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/usertel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入手机号"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="班级:"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userclass"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入班级"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:textColor="#000000"
            android:layout_marginTop="40dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textSize="25sp"
            android:textAllCaps="false"
            android:text="确认"
            android:id="@+id/reday"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:textColor="#000000"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textSize="25sp"
            android:textAllCaps="false"
            android:text="取消"
            android:id="@+id/back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
package com.example.clock;

import static com.example.clock.R.id.userNames;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clock.db.DatabaseHelper;
import com.example.clock.jiben.User;
import com.example.clock.jiben.User;

import java.util.ArrayList;

public class Register extends AppCompatActivity {

    private DatabaseHelper mSQLite;

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

        //找到各个控件
        Button btn_ready = findViewById(R.id.reday);
        Button btn_back = findViewById(R.id.back);
        EditText ed_name = findViewById(R.id.userName);
        EditText ed_password = findViewById(R.id.userpassword);
         EditText ed_name1 = findViewById(R.id.userNames);
         EditText ed_tel= findViewById(R.id.usertel);
        EditText ed_class = findViewById(R.id.userclass);

        //注册监听事件
        btn_ready.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入的用户名和密码
                String name = ed_name.getText().toString().trim();
                String password = ed_password.getText().toString().trim();
                String name1 =ed_name1.getText().toString().trim();
                String tel = ed_tel.getText().toString().trim();
                String classs=ed_class.getText().toString().trim();

                //获取数据库数据,判断用户名是否已存在
                ArrayList<User> data = mSQLite.getAllDATA();
                boolean flag = false;
                for(int i = 0; i < data.size(); i++){
                    User userdata = data.get(i);
                    if(name.equals(userdata.getName())){
                        flag = true;
                        break;
                    }else{
                        flag = false;
                    }
                }
                //判断用户名和密码是否为空
                if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
                    if(!flag){
                        mSQLite.insert(name, password,name1,tel,classs);
                        Intent intent1 = new Intent(Register.this, Login.class);
                        startActivity(intent1);
                        finish();
                        Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(Register.this, "用户名已被注册", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(Register.this, "用户名与密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //监听返回按钮
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(Register.this, Login.class);
                startActivity(intent2);
                finish();
            }
        });

        mSQLite = new DatabaseHelper(Register.this);
    }
}
package com.example.clock.db;



import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

import com.example.clock.jiben.User;
import com.example.clock.jiben.User;

import java.util.ArrayList;
import java.util.jar.Attributes;

import javax.xml.namespace.NamespaceContext;

public class DatabaseHelper extends SQLiteOpenHelper {
    //创建一个数据库
    private SQLiteDatabase db;

    public DatabaseHelper(@Nullable Context context) {
        super(context, "db_test", null, 1);
        db = getReadableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        //在第一次创建数据库的时候,创建一些字段
        String sql = "create table user(_id integer, name varchar(50), password varchar(40),names varchar(20),tel varchar(20),class varchar(20))";
        db.execSQL(sql);//sql语句的执行函数
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //如果这个表中存在user,我们可以先把他去掉,然后重新创建
        String sql = "DROP TABLE IF EXISTS user";
        db.execSQL(sql);
        onCreate(db);
    }
    //为使项目结构更加紧凑,我们在此类中编写增删改查的函数,因为只有登录和注册界面,因此只涉及到写入数据库insert和query的操作
    public void insert(String name,String password ,String namele,String tel,String classs){
        db.execSQL("insert into user(name,password,names,tel,class)VALUES(?,?,?,?,?)",new Object[]{name,password,namele,tel,classs});
    }

    public ArrayList<User> getAllDATA(){//查询数据库
        ArrayList<User> list = new ArrayList<User>();
        //查询数据库中的数据,并将这些数据按照降序的情况排列
        Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
        while(cursor.moveToNext()){
            int index_name = cursor.getColumnIndex("name");
            int index_password = cursor.getColumnIndex("password");
            int index_names = cursor.getColumnIndex("names");
            int index_tel = cursor.getColumnIndex("tel");
            int index_classs=cursor.getColumnIndex("class");
            String name = cursor.getString(index_name);
            String password = cursor.getString(index_password);
            String names = cursor.getString(index_names);
            String tel = cursor.getString(index_tel);
            String classs = cursor.getString(index_classs);
            list.add(new User(name,password,names,tel,classs));
        }
        return list;
    }
}

 

posted @ 2023-03-02 18:51  一个小虎牙  阅读(22)  评论(0编辑  收藏  举报