随笔 - 79  文章 - 0  评论 - 0  阅读 - 3201

第三周星期五每日总结

  今日继续Android studio的学习,本日简单的学习了将数据存入数据库的操作,实现了第一次个人作业注册页面的编写及功能的实现,使用内置SQLite数据库能够将数据存入.

代码实现:

Register.java

复制代码
package com.example.null001;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    EditText usename,usepwd,usepwd2;
    Button submit;
    Mysql mysql;
    SQLiteDatabase db;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        usename = this.findViewById(R.id.usename);                //用户名编辑框
        usepwd =  this.findViewById(R.id.usepwd);                //设置初始密码编辑框
        usepwd2 = this.findViewById(R.id.usepwd2);                //二次输入密码编辑框
        submit =   this.findViewById(R.id.submit);                //注册按钮
        mysql = new Mysql(this,"Userinfo",null,1);      //建数据库
        db = mysql.getReadableDatabase();
        sp = this.getSharedPreferences("useinfo",this.MODE_PRIVATE);


        submit.setOnClickListener(new View.OnClickListener() {
            boolean flag = true;            //判断用户是否已存在的标志位
            @Override
            public void onClick(View v) {
                String name = usename.getText().toString();                //用户名
                String pwd01 = usepwd.getText().toString();                //密码
                String pwd02 = usepwd2.getText().toString();            //二次输入的密码
                String sex = "";                                        //性别
                if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){
                    Toast.makeText(Register.this, "用户名或密码不能为空!!", Toast.LENGTH_LONG).show();
                }
                else{
                    Cursor cursor = db.query("logins",new String[]{"usname"},null,null,null,null,null);

                    while (cursor.moveToNext()){
                        if(cursor.getString(0).equals(name)){
                            flag = false;
                            break;
                        }
                    }
                    if(flag==true){                                             //判断用户是否已存在
                        //if (pwd01.equals(pwd02)) {                                //判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致
                            ContentValues cv = new ContentValues();
                            cv.put("usname",name);
                            cv.put("uspwd",pwd01);
                            db.insert("logins",null,cv);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("usname",name);
                            editor.putString("uspwd",pwd01);
                            editor.commit();
                            Intent intent = new Intent();
                            intent.setClass(Register.this,MainActivity.class);      //跳转到登录页面
                            startActivity(intent);
                            Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_LONG).show();
                       }
                        else {
                          Toast.makeText(Register.this, "密码不一致!", Toast.LENGTH_LONG).show();            //提示密码不一致
                        }
                    }
                    else{
                        Toast.makeText(Register.this, "用户已存在!", Toast.LENGTH_LONG).show();            //提示密码不一致
                    }

                }
            }


        });
    }
}
复制代码

activity_register.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".Register">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!-- 用户名部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="姓名:" />

        <EditText
            android:id="@+id/usename"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType=""
            android:text=""
            />
        <!-- 密码部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="密 码:"
            />

        <EditText
            android:id="@+id/usepwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"

            />
        <!-- 确认密码部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="班级:"
            />

        <EditText
            android:id="@+id/usepwd2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <Button
            android:id="@+id/submit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="注册" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码

Mysql.java//数据库

复制代码
package com.example.null001;

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

import androidx.annotation.Nullable;
public class Mysql extends SQLiteOpenHelper {
    public Mysql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table logins(id integer primary key autoincrement,usname text,uspwd text)";
        db.execSQL(sql);
    }

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

    }
}
复制代码

 

posted on   樱华旧梦  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示