赞
踩
在上一节,已经完成了登录界面的各项功能,接下来是注册界面各项功能的实现以及与登录界面之间的跳转功能,在这里还需借助数据库来存储输入的数据并将数据传输到欢迎界面。
在创建本地数据库后,用户通过I/O流的形式把从键盘输入的账户和密码直接存储到本地数据库,而且会将注册好的账户密码直接写入到登录界面,便捷用户直接登录,节约时间,用户名则将送到欢迎界面,并展示为“欢迎您!+‘用户名’”。
activity_register.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bj3"
- tools:context=".Register">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tv"
- android:layout_width="match_parent"
- android:layout_height="120dp"
- android:gravity="center"
- android:text="注册"
- android:textSize="35sp"
- android:textColor="#516D2C"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal"
- android:layout_marginTop="10dp">
-
- <ImageView
- android:layout_width="30dp"
- android:layout_height="40dp"
- android:layout_gravity="center"
- android:src="@drawable/one"
- android:layout_marginRight="5dp"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:text="用 户:"
- android:textSize="18sp"
- android:gravity="center"
- android:textColor="#000000"/>
-
- <EditText
- android:id="@+id/usename"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:hint="请输入用户名"
- android:background="@drawable/editext_selector"
- android:textSize="18sp"
- android:textColor="#000000"
- android:inputType="text"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="5dp"
- android:gravity="left|center"
- android:maxLength="20"
- android:paddingLeft="10dp"/>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal"
- android:layout_marginTop="10dp">
-
- <ImageView
- android:layout_width="30dp"
- android:layout_height="50dp"
- android:src="@drawable/two"
- android:layout_marginRight="5dp"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:text="密 码:"
- android:textSize="18sp"
- android:gravity="center"
- android:textColor="#000000"/>
-
- <EditText
- android:id="@+id/usepwd"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:hint="请输入密码"
- android:background="@drawable/editext_selector"
- android:textSize="18sp"
- android:textColor="#000000"
- android:inputType="textPassword"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="5dp"
- android:gravity="left|center"
- android:maxLength="20"
- android:paddingLeft="10dp"/>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal"
- android:layout_marginTop="10dp">
-
- <ImageView
- android:layout_width="30dp"
- android:layout_height="50dp"
- android:src="@drawable/three"
- android:layout_marginRight="5dp"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:text="确认密码:"
- android:textSize="18sp"
- android:gravity="center"
- android:textColor="#000000"/>
-
- <EditText
- android:id="@+id/usepwd2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:hint="请确认密码"
- android:background="@drawable/editext_selector"
- android:textSize="18sp"
- android:textColor="#000000"
- android:inputType="textPassword"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="5dp"
- android:gravity="left|center"
- android:maxLength="20"
- android:paddingLeft="10dp"/>
- </LinearLayout>
-
- <Button
- android:id="@+id/submit"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:text="注册"
- android:gravity="center"
- android:textSize="20sp"
- android:textColor="#FFFFFF"
- android:background="@drawable/btn_selector"
- android:layout_marginTop="15dp"/>
- </LinearLayout>
- </LinearLayout>
注册按钮有两个,一个是在登录界面,另一个在注册界面。登录界面的注册按钮会实现注册界面的跳转,而注册界面的按钮会在编辑框为空的时候,弹窗提示“用户名和密码不能为空!”;在编辑框不为空,但用户名已注册过时,弹窗提示“用户已存在!”;在为新用户,但两次密码输入不一致时,弹窗提示“密码不一致!”;如果是一个新用户,且不存在上述情况,账户将注册成功,如下图。
Java代码实现
(1)登录界面按钮(登录界面跳转注册界面)
- //注册事件
- btnreg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClass(MainActivity.this,Register.class); //跳转到注册页面
- startActivity(intent);
- Toast.makeText(MainActivity.this,"前往注册!",Toast.LENGTH_SHORT).show();
- }
- });
(2)注册界面按钮
- 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(); //二次输入的密码
- 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(); //提示密码不一致
- }
-
- }
- }
- });
- }
构建本地数据库,构造方法中有四个参数,第一个参数(context)用于操作数据库,第二个参数(name)用于指定数据库的名称,第三个参数(factory)一般传入null即可,第四个参数(version)用于指定数据库的版本号,在这里采用的是sqlite中四种常用的文本型(text)数据类型,因为SQLiteOpenHelper类是一个抽象类,所以它的两个抽象方法(onCreate方法和onUpgrade方法)都必须写出来,并用execSQL()方法用来执行数据库语句。
注册成功的账号密码将会保存在Device File Explorer视图下的data/data/com.example.test06/databases文件中,如下图。
Java代码实现
- package com.example.test06;
-
- 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) {
-
- }
- }
- package com.example.test06;
-
- 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(); //二次输入的密码
- 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(); //提示密码不一致
- }
-
- }
- }
- });
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。