Accounting APP: Xiaoha Accounting 2 - Making of the registration page

Project Introduction:

        Xiaoha Bookkeeping is an app for bookkeeping, based on the Android Studio development tool, using the Java language for development, and using litepal and Alibaba Cloud database to add, delete, check and modify data, and display it on the app interface in the form of icons. The app can clearly display the income and expenditure situation, and display the monthly income and expenditure situation in the form of a chart; at the same time, it can record consumption purposes, and the project can accurately track the income and expenditure time and balance of each payment, and can set a monthly budget. Reminders of exceeding the budget, and weekly, monthly and annual bill statistics, which are more clear and clear to help users analyze their current expenditure and income. At the same time, it has functions such as user login, registration, and modification of avatar.

        Technologies used in this project: LitePal database, Alibaba Cloud RDS database, MPAndroidChart chart library, some custom controls, etc.

        Support Android 5.0 and above.

        This article will introduce the creation of the registration page of Xiaoha Accounting.

Renderings:

source code:

activity_register.xml

This file is the layout file of the registration page. The outermost layer is a RelativeLayout, and the interior is divided into five parts. The topmost is the top RelativeLayout, which stores the content of the top title bar. The first LinearLayout stores the content of the registration form, the second LinearLayout stores the content of the error prompt, and the latter two ImageViews store the registration button and the small eyes for displaying the hidden password respectively.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!--顶部-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/top_color"
        android:orientation="horizontal"
        android:paddingBottom="8sp"
        android:paddingTop="36sp">

        <ImageView
            android:id="@+id/button_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="18sp"
            android:layout_marginTop="10sp"
            android:src="@drawable/ic_back_white_24" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8sp"
            android:text="使用手机号注册"
            android:textColor="@color/white"
            android:textSize="@dimen/top_title" />

    </RelativeLayout>
    <!--注册内容-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100sp">
        <!--用户名-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用&#8194;户&#8194;名:"
                android:textColor="@color/grey_5"
                android:textSize="@dimen/register_title"/>

            <EditText
                android:id="@+id/register_name"
                android:layout_width="220sp"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:gravity="left"
                android:maxLength="20"
                android:textSize="16sp"
                android:textColor="#000000"/>

        </LinearLayout>
        <!--手机号-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="8sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="手&#8194;机&#8194;号:"
                android:textColor="@color/grey_5"
                android:textSize="@dimen/register_title"/>
            <EditText
                android:id="@+id/register_phone"
                android:layout_width="220sp"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:gravity="left"
                android:maxLength="11"
                android:textSize="16sp"
                android:textColor="#000000"/>

        </LinearLayout>
        <!--验证码-->
        <!--<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="8sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="验&#8194;证&#8194;码:"
                android:textColor="@color/grey_5"
                android:textSize="@dimen/register_title"/>
            <EditText
                android:id="@+id/register_code"
                android:layout_width="110sp"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:gravity="left"
                android:textSize="16sp"
                android:textColor="@color/black"/>
            <LinearLayout
                android:id="@+id/get_code"
                android:layout_width="wrap_content"
                android:layout_height="40sp"
                android:layout_alignParentRight="true"
                android:orientation="vertical"
                style="@style/InputBoxStyleCode">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="获取验证码"
                    android:textSize="14sp"
                    android:textColor="@color/white" />
            </LinearLayout>

        </LinearLayout>-->
        <!--密码-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="8sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密&#8194;&#8194;&#8194;&#8194;码:"
                android:textColor="@color/grey_5"
                android:textSize="@dimen/register_title"/>
            <EditText
                android:id="@+id/register_password"
                android:layout_width="220sp"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:singleLine="true"
                android:gravity="left"
                android:maxLength="16"
                android:textSize="16sp"
                android:textColor="#000000"/>

        </LinearLayout>
        <!--确认密码-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="8sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确认密码:"
                android:textColor="@color/grey_5"
                android:textSize="@dimen/register_title"/>
            <EditText
                android:id="@+id/register_password2"
                android:layout_width="220sp"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:singleLine="true"
                android:gravity="left"
                android:maxLength="16"
                android:textSize="16sp"
                android:textColor="#000000"/>


        </LinearLayout>


    </LinearLayout>
    <!--提示内容-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name_exist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="136sp"
            android:text="用户名已存在"
            android:textColor="@color/red"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/phone_exist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="32sp"
            android:text="手机号不合法"
            android:textColor="@color/red"
            android:visibility="invisible" />

    </LinearLayout>
    <!--注册按钮-->
    <ImageView
        android:id="@+id/register_button"
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="380sp"
        android:background="@drawable/login_login_button" />
    <!--密码隐藏与显示-->
    <ImageView
        android:id="@+id/register_eye"
        android:layout_width="20sp"
        android:layout_height="20sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="260sp"
        android:layout_marginRight="74sp"
        android:background="@drawable/eye_none" />

</RelativeLayout>

RegisterActivity.java

        This file is a registration activity, which uses the Alibaba Cloud database. The configuration of the Alibaba Cloud database will be introduced in detail in subsequent articles. I still retain the registration code of the local database in the form of comments for some functions. If there is no cloud database, you can use the local database first. for debugging. (MyAppCompatActivity.java see article 1 - welcome page production )

        For the registration function of this app, you need to fill in the user name, mobile phone number, password and confirmation password (the verification code function is left in the xml layout, but it has not been developed yet). The user name and mobile phone number are set to be non-repeatable, and the mobile phone number must be legal. The password and confirmation password must be the same. So we need to add events to judge the user name, mobile phone number and password entered by the user.

Username detection already exists function:

Here are the codes of two forms of databases (local and cloud databases) for your choice.

 /*查询用户名是否存在*/
    private void name_is_exist(boolean hasFocus){
        if(!hasFocus){
            /*阿里云数据库*/
            name = nameEdit.getText().toString();
            final Handler handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message message) {
                    if (bo) {
                        name_exist.setVisibility(View.VISIBLE);
                    } else{
                        name_exist.setVisibility(View.INVISIBLE);
                    }
                    return false;
                }
            });
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = new Message();
                    try {
                        bo = Database_Sql.querySelect_exist_x("user_name",name);
                    } catch (SQLException throwables) {
                        bo = false;
                        throwables.printStackTrace();
                    }
                    handler.sendMessage(msg);
                }
            }).start();
            /*本地数据库
            name = nameEdit.getText().toString();
            name_exist.setVisibility(View.INVISIBLE);
            List<Users> users = LitePal.findAll(Users.class,true);
            for (Users user : users) {
                if (user.getUser_name().equals(name)) {
                    name_exist.setVisibility(View.VISIBLE);
                }
            }*/
        }
    }

Determine whether the mobile phone number has been registered function:

Here are also the codes of the two databases for you to choose.

/*查询手机号是否注册*/
    private void phone_is_exist(boolean hasFocus){
        /*阿里云数据库*/
        if(!hasFocus){
            phone_exist.setVisibility(View.INVISIBLE);
            phone = phoneEdit.getText().toString();
            if(!isPhone(phone)){
                phone_exist.setText("手机号不合法");
                phone_exist.setVisibility(View.VISIBLE);
            }else {
                final Handler handler = new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(@NonNull Message message) {
                        if (boo) {
                            phone_exist.setText("该手机号已注册");
                            phone_exist.setVisibility(View.VISIBLE);
                        } else {
                            phone_exist.setVisibility(View.INVISIBLE);
                        }
                        return false;
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        try {
                            boo = Database_Sql.querySelect_exist_x("user_phone",phone);
                        } catch (SQLException throwables) {
                            boo = false;
                            throwables.printStackTrace();
                        }
                        handler.sendMessage(msg);
                    }
                }).start();
            }
            /*本地数据库
            phone = phoneEdit.getText().toString();
            phone_exist.setVisibility(View.INVISIBLE);
            if(!isPhone(phone)){
                phone_exist.setText("手机号不合法");
                phone_exist.setVisibility(View.VISIBLE);
            }else {
                List<Users> users = LitePal.findAll(Users.class);
                for (Users user : users) {
                    if (user.getUser_phone().equals(phone)) {
                        phone_exist.setText("该手机号已注册");
                        phone_exist.setVisibility(View.VISIBLE);
                    }
                }
            }*/
        }
    }

Determine whether it is a mobile phone number function:

/*判断是否是手机号*/
private static boolean isPhone(String phone){
     String telRegex = "^[1][3-9]\\d{9}$";
     //"[1]"代表第1位为数字1,"[3456789]"代表第二位可以为3-9中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
     return phone.matches(telRegex);
}

The following is the complete code:

package com.example.xiaohaaccounting.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;


import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xiaohaaccounting.Entity_class.Users;
import com.example.xiaohaaccounting.R;
import com.example.xiaohaaccounting.Utils_class.ActivityCollector;
import com.example.xiaohaaccounting.Utils_class.Database_Sql;
import com.example.xiaohaaccounting.Utils_class.MyAppCompatActivity;

import org.litepal.LitePal;

import java.sql.SQLException;

public class RegisterActivity extends MyAppCompatActivity {

    private ImageView register_eye;
    private EditText nameEdit;
    private EditText phoneEdit;
    //private EditText codeEdit;
    private EditText passwordEdit;
    private EditText password2Edit;
    //private LinearLayout get_code;
    private TextView name_exist;
    private TextView phone_exist;

    private String name;
    private String phone;
    private String password;
    private String password2;
    private String code;
    private Boolean bo = false;
    private Boolean boo = false;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);

        /*返回登录页面*/
        ImageView button_back = (ImageView) findViewById(R.id.button_back);
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        initView();//初始化控件

        /*检测用户名是否存在*/
        nameEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                name_is_exist(hasFocus);
            }

        });
        /*检测手机号是否合法,是否注册*/
        phoneEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                phone_is_exist(hasFocus);
            }
        });

        /*获取验证码*//*
        get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });*/
        /*显示或隐藏密码*/
        register_eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(password2Edit.getInputType() == 129){
                    register_eye.setBackground(getDrawable(R.drawable.eye));
                    password2Edit.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else{
                    register_eye.setBackground(getDrawable(R.drawable.eye_none));
                    password2Edit.setInputType(129);
                }
            }
        });
        /*注册*/
        ImageView register_button = (ImageView) findViewById(R.id.register_button);
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    register();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
    }
    /*初始化控件*/
    private void initView(){
        nameEdit = (EditText) findViewById(R.id.register_name);
        phoneEdit = (EditText) findViewById(R.id.register_phone);
        //codeEdit = (EditText) findViewById(R.id.register_code);
        passwordEdit = (EditText) findViewById(R.id.register_password);
        password2Edit = (EditText) findViewById(R.id.register_password2);
        //get_code = (LinearLayout) findViewById(R.id.get_code);
        name_exist = (TextView) findViewById(R.id.name_exist);
        phone_exist = (TextView) findViewById(R.id.phone_exist);
        register_eye = (ImageView) findViewById(R.id.register_eye);
    }

    /*查询用户名是否存在*/
    private void name_is_exist(boolean hasFocus){
        if(!hasFocus){
            /*阿里云数据库*/
            name = nameEdit.getText().toString();
            final Handler handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message message) {
                    if (bo) {
                        name_exist.setVisibility(View.VISIBLE);
                    } else{
                        name_exist.setVisibility(View.INVISIBLE);
                    }
                    return false;
                }
            });
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = new Message();
                    try {
                        bo = Database_Sql.querySelect_exist_x("user_name",name);
                    } catch (SQLException throwables) {
                        bo = false;
                        throwables.printStackTrace();
                    }
                    handler.sendMessage(msg);
                }
            }).start();
            /*本地数据库
            name = nameEdit.getText().toString();
            name_exist.setVisibility(View.INVISIBLE);
            List<Users> users = LitePal.findAll(Users.class,true);
            for (Users user : users) {
                if (user.getUser_name().equals(name)) {
                    name_exist.setVisibility(View.VISIBLE);
                }
            }*/
        }
    }
    /*查询手机号是否注册*/
    private void phone_is_exist(boolean hasFocus){
        /*阿里云数据库*/
        if(!hasFocus){
            phone_exist.setVisibility(View.INVISIBLE);
            phone = phoneEdit.getText().toString();
            if(!isPhone(phone)){
                phone_exist.setText("手机号不合法");
                phone_exist.setVisibility(View.VISIBLE);
            }else {
                final Handler handler = new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(@NonNull Message message) {
                        if (boo) {
                            phone_exist.setText("该手机号已注册");
                            phone_exist.setVisibility(View.VISIBLE);
                        } else {
                            phone_exist.setVisibility(View.INVISIBLE);
                        }
                        return false;
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        try {
                            boo = Database_Sql.querySelect_exist_x("user_phone",phone);
                        } catch (SQLException throwables) {
                            boo = false;
                            throwables.printStackTrace();
                        }
                        handler.sendMessage(msg);
                    }
                }).start();
            }
            /*本地数据库
            phone = phoneEdit.getText().toString();
            phone_exist.setVisibility(View.INVISIBLE);
            if(!isPhone(phone)){
                phone_exist.setText("手机号不合法");
                phone_exist.setVisibility(View.VISIBLE);
            }else {
                List<Users> users = LitePal.findAll(Users.class);
                for (Users user : users) {
                    if (user.getUser_phone().equals(phone)) {
                        phone_exist.setText("该手机号已注册");
                        phone_exist.setVisibility(View.VISIBLE);
                    }
                }
            }*/
        }
    }
    /*注册*/
    private void register() throws SQLException {

        name = nameEdit.getText().toString();
        //code = codeEdit.getText().toString();
        password = passwordEdit.getText().toString();
        password2 = password2Edit.getText().toString();

        if(TextUtils.isEmpty(name)){
            Toast.makeText(getApplicationContext(),"用户名不能为空",Toast.LENGTH_SHORT).show();
        } else if(TextUtils.isEmpty(phone)){
            Toast.makeText(getApplicationContext(),"手机号不能为空",Toast.LENGTH_SHORT).show();
        } else if(!isPhone(phone)){
            Toast.makeText(getApplicationContext(),"手机号不合法",Toast.LENGTH_SHORT).show();
        } else if(boo){
            Toast.makeText(getApplicationContext(),"手机号已注册",Toast.LENGTH_SHORT).show();
        }/* else if(TextUtils.isEmpty(code)){
            Toast.makeText(getApplicationContext(),"验证码不能为空",Toast.LENGTH_SHORT).show();
        } */else if(TextUtils.isEmpty(password)){
            Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show();
        } else if(TextUtils.isEmpty(password2)){
            Toast.makeText(getApplicationContext(),"两次密码不一致",Toast.LENGTH_SHORT).show();
        } else if(password.equals(password2)){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Database_Sql.insertIntoData(name,phone,password);//调用插入数据库语句
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            Users user = new Users();
            user.setUser_name(name);
            user.setUser_password(password);
            user.setUser_phone(phone);
            user.save();

            Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
            finish();
        } else if(password != password2){
            AlertDialog.Builder register_dialog = new AlertDialog.Builder(RegisterActivity.this);
            register_dialog.setTitle("警告");
            register_dialog.setMessage("两次密码不一致,请重新输入密码");
            register_dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            });
            register_dialog.show();
        }
    }
    /*判断是否是手机号*/
    private static boolean isPhone(String phone){
        String telRegex = "^[1][3-9]\\d{9}$";
        //"[1]"代表第1位为数字1,"[3456789]"代表第二位可以为3-9中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
        return phone.matches(telRegex);
    }

}

Guess you like

Origin blog.csdn.net/Me_Rui/article/details/125926185