当前位置:   article > 正文

Android学习(二)--用户登录注册界面(界面跳转+背景音乐)_android登录注册跳转的代码

android登录注册跳转的代码

1.回顾

在上一节,已经完成了登录界面的各项功能,接下来是注册界面各项功能的实现以及与登录界面之间的跳转功能,在这里还需借助数据库来存储输入的数据并将数据传输到欢迎界面。

2.注册界面

2.1界面展示

 2.2注册界面功能简介

在创建本地数据库后,用户通过I/O流的形式把从键盘输入的账户和密码直接存储到本地数据库,而且会将注册好的账户密码直接写入到登录界面,便捷用户直接登录,节约时间,用户名则将送到欢迎界面,并展示为“欢迎您!+‘用户名’”。

2.3界面代码

activity_register.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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:background="@drawable/bj3"
  7. tools:context=".Register">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:orientation="vertical">
  12. <TextView
  13. android:id="@+id/tv"
  14. android:layout_width="match_parent"
  15. android:layout_height="120dp"
  16. android:gravity="center"
  17. android:text="注册"
  18. android:textSize="35sp"
  19. android:textColor="#516D2C"/>
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="50dp"
  23. android:orientation="horizontal"
  24. android:layout_marginTop="10dp">
  25. <ImageView
  26. android:layout_width="30dp"
  27. android:layout_height="40dp"
  28. android:layout_gravity="center"
  29. android:src="@drawable/one"
  30. android:layout_marginRight="5dp"/>
  31. <TextView
  32. android:layout_width="wrap_content"
  33. android:layout_height="match_parent"
  34. android:text="用 户:"
  35. android:textSize="18sp"
  36. android:gravity="center"
  37. android:textColor="#000000"/>
  38. <EditText
  39. android:id="@+id/usename"
  40. android:layout_width="match_parent"
  41. android:layout_height="match_parent"
  42. android:hint="请输入用户名"
  43. android:background="@drawable/editext_selector"
  44. android:textSize="18sp"
  45. android:textColor="#000000"
  46. android:inputType="text"
  47. android:layout_marginTop="5dp"
  48. android:layout_marginBottom="5dp"
  49. android:gravity="left|center"
  50. android:maxLength="20"
  51. android:paddingLeft="10dp"/>
  52. </LinearLayout>
  53. <LinearLayout
  54. android:layout_width="match_parent"
  55. android:layout_height="50dp"
  56. android:orientation="horizontal"
  57. android:layout_marginTop="10dp">
  58. <ImageView
  59. android:layout_width="30dp"
  60. android:layout_height="50dp"
  61. android:src="@drawable/two"
  62. android:layout_marginRight="5dp"/>
  63. <TextView
  64. android:layout_width="wrap_content"
  65. android:layout_height="match_parent"
  66. android:text="密 码:"
  67. android:textSize="18sp"
  68. android:gravity="center"
  69. android:textColor="#000000"/>
  70. <EditText
  71. android:id="@+id/usepwd"
  72. android:layout_width="match_parent"
  73. android:layout_height="match_parent"
  74. android:hint="请输入密码"
  75. android:background="@drawable/editext_selector"
  76. android:textSize="18sp"
  77. android:textColor="#000000"
  78. android:inputType="textPassword"
  79. android:layout_marginTop="5dp"
  80. android:layout_marginBottom="5dp"
  81. android:gravity="left|center"
  82. android:maxLength="20"
  83. android:paddingLeft="10dp"/>
  84. </LinearLayout>
  85. <LinearLayout
  86. android:layout_width="match_parent"
  87. android:layout_height="50dp"
  88. android:orientation="horizontal"
  89. android:layout_marginTop="10dp">
  90. <ImageView
  91. android:layout_width="30dp"
  92. android:layout_height="50dp"
  93. android:src="@drawable/three"
  94. android:layout_marginRight="5dp"/>
  95. <TextView
  96. android:layout_width="wrap_content"
  97. android:layout_height="match_parent"
  98. android:text="确认密码:"
  99. android:textSize="18sp"
  100. android:gravity="center"
  101. android:textColor="#000000"/>
  102. <EditText
  103. android:id="@+id/usepwd2"
  104. android:layout_width="match_parent"
  105. android:layout_height="match_parent"
  106. android:hint="请确认密码"
  107. android:background="@drawable/editext_selector"
  108. android:textSize="18sp"
  109. android:textColor="#000000"
  110. android:inputType="textPassword"
  111. android:layout_marginTop="5dp"
  112. android:layout_marginBottom="5dp"
  113. android:gravity="left|center"
  114. android:maxLength="20"
  115. android:paddingLeft="10dp"/>
  116. </LinearLayout>
  117. <Button
  118. android:id="@+id/submit"
  119. android:layout_width="match_parent"
  120. android:layout_height="50dp"
  121. android:text="注册"
  122. android:gravity="center"
  123. android:textSize="20sp"
  124. android:textColor="#FFFFFF"
  125. android:background="@drawable/btn_selector"
  126. android:layout_marginTop="15dp"/>
  127. </LinearLayout>
  128. </LinearLayout>

2.3注册按钮点击事件

注册按钮有两个,一个是在登录界面,另一个在注册界面。登录界面的注册按钮会实现注册界面的跳转,而注册界面的按钮会在编辑框为空的时候,弹窗提示“用户名和密码不能为空!”;在编辑框不为空,但用户名已注册过时,弹窗提示“用户已存在!”;在为新用户,但两次密码输入不一致时,弹窗提示“密码不一致!”;如果是一个新用户,且不存在上述情况,账户将注册成功,如下图。

Java代码实现

(1)登录界面按钮(登录界面跳转注册界面)

  1. //注册事件
  2. btnreg.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. Intent intent = new Intent();
  6. intent.setClass(MainActivity.this,Register.class); //跳转到注册页面
  7. startActivity(intent);
  8. Toast.makeText(MainActivity.this,"前往注册!",Toast.LENGTH_SHORT).show();
  9. }
  10. });

 (2)注册界面按钮

  1. submit.setOnClickListener(new View.OnClickListener() {
  2. boolean flag = true; //判断用户是否已存在的标志位
  3. @Override
  4. public void onClick(View v) {
  5. String name = usename.getText().toString(); //用户名
  6. String pwd01 = usepwd.getText().toString(); //密码
  7. String pwd02 = usepwd2.getText().toString(); //二次输入的密码
  8. if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){
  9. Toast.makeText(Register.this, "用户名或密码不能为空!!", Toast.LENGTH_LONG).show();
  10. }
  11. else{
  12. Cursor cursor = db.query("logins",new String[]{"usname"},null,null,null,null,null);
  13. while (cursor.moveToNext()){
  14. if(cursor.getString(0).equals(name)){
  15. flag = false;
  16. break;
  17. }
  18. }
  19. if(flag==true){ //判断用户是否已存在
  20. if (pwd01.equals(pwd02)) { //判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致
  21. ContentValues cv = new ContentValues();
  22. cv.put("usname",name);
  23. cv.put("uspwd",pwd01);
  24. db.insert("logins",null,cv);
  25. SharedPreferences.Editor editor = sp.edit();
  26. editor.putString("usname",name);
  27. editor.putString("uspwd",pwd01);
  28. editor.commit();
  29. Intent intent = new Intent();
  30. intent.setClass(Register.this,MainActivity.class); //跳转到登录页面
  31. startActivity(intent);
  32. Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_LONG).show();
  33. }
  34. else {
  35. Toast.makeText(Register.this, "密码不一致!", Toast.LENGTH_LONG).show(); //提示密码不一致
  36. }
  37. }
  38. else{
  39. Toast.makeText(Register.this, "用户已存在!", Toast.LENGTH_LONG).show(); //提示密码不一致
  40. }
  41. }
  42. }
  43. });
  44. }

 2.4本地数据库

构建本地数据库,构造方法中有四个参数,第一个参数(context)用于操作数据库,第二个参数(name)用于指定数据库的名称,第三个参数(factory)一般传入null即可,第四个参数(version)用于指定数据库的版本号,在这里采用的是sqlite中四种常用的文本型(text)数据类型,因为SQLiteOpenHelper类是一个抽象类,所以它的两个抽象方法(onCreate方法和onUpgrade方法)都必须写出来,并用execSQL()方法用来执行数据库语句。

注册成功的账号密码将会保存在Device File Explorer视图下的data/data/com.example.test06/databases文件中,如下图

 Java代码实现

  1. package com.example.test06;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import androidx.annotation.Nullable;
  6. public class Mysql extends SQLiteOpenHelper {
  7. public Mysql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
  8. super(context, name, factory, version);
  9. }
  10. @Override
  11. public void onCreate(SQLiteDatabase db) {
  12. String sql = "create table logins(id integer primary key autoincrement,usname text,uspwd text)";
  13. db.execSQL(sql);
  14. }
  15. @Override
  16. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  17. }
  18. }

2.5Java源码

  1. package com.example.test06;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.ContentValues;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. public class Register extends AppCompatActivity {
  14. EditText usename,usepwd,usepwd2;
  15. Button submit;
  16. Mysql mysql;
  17. SQLiteDatabase db;
  18. SharedPreferences sp;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_register);
  23. usename = this.findViewById(R.id.usename); //用户名编辑框
  24. usepwd = this.findViewById(R.id.usepwd); //设置初始密码编辑框
  25. usepwd2 = this.findViewById(R.id.usepwd2); //二次输入密码编辑框
  26. submit = this.findViewById(R.id.submit); //注册按钮
  27. mysql = new Mysql(this,"Userinfo",null,1); //建立数据库
  28. db = mysql.getReadableDatabase();
  29. sp = this.getSharedPreferences("useinfo",this.MODE_PRIVATE);
  30. submit.setOnClickListener(new View.OnClickListener() {
  31. boolean flag = true; //判断用户是否已存在的标志位
  32. @Override
  33. public void onClick(View v) {
  34. String name = usename.getText().toString(); //用户名
  35. String pwd01 = usepwd.getText().toString(); //密码
  36. String pwd02 = usepwd2.getText().toString(); //二次输入的密码
  37. if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){
  38. Toast.makeText(Register.this, "用户名或密码不能为空!!", Toast.LENGTH_LONG).show();
  39. }
  40. else{
  41. Cursor cursor = db.query("logins",new String[]{"usname"},null,null,null,null,null);
  42. while (cursor.moveToNext()){
  43. if(cursor.getString(0).equals(name)){
  44. flag = false;
  45. break;
  46. }
  47. }
  48. if(flag==true){ //判断用户是否已存在
  49. if (pwd01.equals(pwd02)) { //判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致
  50. ContentValues cv = new ContentValues();
  51. cv.put("usname",name);
  52. cv.put("uspwd",pwd01);
  53. db.insert("logins",null,cv);
  54. SharedPreferences.Editor editor = sp.edit();
  55. editor.putString("usname",name);
  56. editor.putString("uspwd",pwd01);
  57. editor.commit();
  58. Intent intent = new Intent();
  59. intent.setClass(Register.this,MainActivity.class); //跳转到登录页面
  60. startActivity(intent);
  61. Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_LONG).show();
  62. }
  63. else {
  64. Toast.makeText(Register.this, "密码不一致!", Toast.LENGTH_LONG).show(); //提示密码不一致
  65. }
  66. }
  67. else{
  68. Toast.makeText(Register.this, "用户已存在!", Toast.LENGTH_LONG).show(); //提示密码不一致
  69. }
  70. }
  71. }
  72. });
  73. }
  74. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/705006
推荐阅读
相关标签
  

闽ICP备14008679号