Android Studio 实现注册信息表单验证的源代码(实现账号,密码

发布于 2021-3-31 13:41
浏览
0收藏

一、界面布局
1、界面样子:

Android Studio 实现注册信息表单验证的源代码(实现账号,密码-开源基础软件社区
2、在activity_main.xml当中创建布局界面

Android Studio 实现注册信息表单验证的源代码(实现账号,密码-开源基础软件社区
3、源代码

<?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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="84dp"
        android:padding="50dp"
        tools:layout_editor_absoluteY="186dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:gravity="center"
                android:text="账号:" />

            <EditText
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:gravity="center"
                android:inputType="textPersonName"
                android:hint="请输入账号"
                />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:gravity="center"
                android:text="密码:" />
            <EditText
                android:id="@+id/password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:gravity="center"
                android:inputType="textPassword"
                android:hint="请输入密码"
                />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:gravity="center"
                android:text="邮箱:" />
            <EditText
                android:id="@+id/email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:gravity="center"
                android:inputType="textEmailAddress"
                android:hint="请输入邮箱"

                />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:gravity="center"
                android:text="手机号:" />
            <EditText
                android:id="@+id/phone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:gravity="center"
                android:inputType="phone"
                android:maxLength="11"
                android:hint="请输入手机号"

                />

        </LinearLayout>

        <Button

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:onClick="register"
            android:text="注册" />

        <Button

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:onClick="reset"
            android:text="重置" />


    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


二、Java源代码

Android Studio 实现注册信息表单验证的源代码(实现账号,密码-开源基础软件社区

package com.example.application22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    EditText name = null;
    EditText password = null;
    EditText email = null;
    EditText phone = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
        email = findViewById(R.id.email);
        phone = findViewById(R.id.phone);

    }

    public void register(View v){
       String namestring = name.getText().toString();
       String passwordstring = password.getText().toString();
       String emailstring = email.getText().toString();
       String phonestring = phone.getText().toString();
        //判断账号
        if(namestring.length() == 0  ){
           Toast.makeText(getApplicationContext(),"账号不能为空",Toast.LENGTH_SHORT).show();
           return;
        }
        if(namestring.length() > 16  ){
            Toast.makeText(getApplicationContext(),"账号必须小于16位",Toast.LENGTH_SHORT).show();
            return;
        }
        if(namestring.length() != 0 && namestring.length() < 8  ){
            Toast.makeText(getApplicationContext(),"账号必须大于8位",Toast.LENGTH_SHORT).show();
            return;
        }
        //判断密码
        if(passwordstring.length() == 0 ){
            Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(passwordstring.length() > 16  ){
            Toast.makeText(getApplicationContext(),"密码必须小于16位",Toast.LENGTH_SHORT).show();
            return;
        }
        if(passwordstring.length() != 0 && passwordstring.length() < 8  ){
            Toast.makeText(getApplicationContext(),"密码必须大于8位",Toast.LENGTH_SHORT).show();
            return;
        }

        //判断邮箱
        if(emailstring.length() == 0 ){
            Toast.makeText(getApplicationContext(),"邮箱不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;

        p = Pattern.compile(regEx1);
        m = p.matcher(emailstring);

        if (!m.matches()){
            Toast.makeText(getApplicationContext(),"邮箱格式不正确",Toast.LENGTH_SHORT).show();
            return;
        }

        //判断电话
        if(phonestring.length() == 0 ){
            Toast.makeText(getApplicationContext(),"电话不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        Pattern p1 = Pattern.compile("^1[3,5,7,8,9][0-9]{9}$");
        Matcher m1 = p1.matcher(phonestring);

        if(!m1.matches()){
            Toast.makeText(getApplicationContext(),"电话格式不正确",Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(getApplicationContext(),"注册成功请登录",Toast.LENGTH_SHORT).show();

    }

    public void reset(View v){
        name.setText("");
        password.setText("");
        email.setText("");
        phone.setText("");
    }


}


————————————————
版权声明:本文为博主「代码100分」的原创文章

分类
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐