Android learning (2) -- user login registration interface (interface jump + background music)

1. Review

In the previous section, the various functions of the login interface have been completed. Next is the realization of various functions of the registration interface and the jump function between the login interface. Here, the database is needed to store the input data and store the data. Transfer to the welcome screen.

2. Registration interface

2.1 Interface display

 2.2 Introduction to Registration Interface Functions

After creating the local database, the user directly stores the account and password input from the keyboard to the local database through the I/O stream, and writes the registered account password directly to the login interface, which is convenient for the user to log in directly and saves time , the username will be sent to the welcome screen and displayed as "Welcome! + 'username'".

2.3 Interface code

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>

2.3 Registration button click event

There are two registration buttons, one is on the login interface and the other is on the registration interface. The registration button on the login interface will realize the jump to the registration interface, and the button on the registration interface will prompt "User name and password cannot be empty!" when the edit box is empty; the edit box is not empty, but the user name If you have already registered, a pop-up window will prompt "User already exists!"; if you are a new user, but the two passwords entered are inconsistent, a pop-up window will prompt "Passwords are inconsistent!"; if you are a new user and the above conditions do not exist, the account will be deleted The registration is successful, as shown in the figure below.

Java code implementation

(1) Login interface button (the login interface jumps to the registration interface)

//注册事件
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) Registration interface button

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();       //提示密码不一致
                }

            }
        }
    });
}

 2.4 Local database

To build a local database, there are four parameters in the construction method. The first parameter (context) is used to operate the database, the second parameter (name) is used to specify the name of the database, and the third parameter (factory) is generally passed in as null. Yes, the fourth parameter (version) is used to specify the version number of the database. Here, four commonly used text data types in sqlite are used. Because the SQLiteOpenHelper class is an abstract class, its two abstract The methods (onCreate method and onUpgrade method) must be written out, and the execSQL() method is used to execute the database statement.

Successfully registered account passwords will be saved in the data/data/com.example.test06/databases file in the Device File Explorer view, as shown in the figure below .

 Java code implementation

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) {

    }
}

2.5 Java source code

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();			//提示密码不一致
                    }

                }
            }
        });
    }
}

Guess you like

Origin blog.csdn.net/qq_62250174/article/details/129126040