第一次冲刺(一)

今天我完成了注册界面的activity的设计。

代码为:

package com.example.hotel.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hotel.Database.DBOpenHelper;
import com.example.hotel.Database.MysqlUser;
import com.example.hotel.MainActivity;
import com.example.hotel.R;

import java.sql.SQLException;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends AppCompatActivity {
    private Button ButtonRegister;
    private Button ButtonGetcode;
    private String searchid;
    private String phone_number;
    private String cord_number;
    private String id;
    private String pwd;
    private String pwd2;
    private String ph;
    private String idnum;
    private static int num=0;
    EventHandler eventHandler;
    private boolean flag=true;
    private EditText EditTextId;
    private EditText EditTextPassword;
    private EditText EditTextPassword2;
    private EditText EditTextPhone;
    private EditText EditTextCode;
    private EditText EditTextIdNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        searchid=null;
        EditTextId=findViewById(R.id.editTextId);
        EditTextPassword=findViewById(R.id.editTextPassword);
        EditTextPassword2=findViewById(R.id.editTextPassword2);
        EditTextPhone=findViewById(R.id.editTextPhone);
        EditTextCode=findViewById(R.id.yzm);
        EditTextIdNumber=findViewById(R.id.editTextIdNumber);
        ButtonGetcode=findViewById(R.id.getcode);
        ButtonGetcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(judPhone())//去掉左右空格获取字符串
                {
                    SMSSDK.getVerificationCode("86",phone_number);
                    EditTextPhone.requestFocus();
                }
            }
        });
        ButtonRegister=findViewById(R.id.buttonRegister);
        ButtonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MysqlUser mysqlUser=new MysqlUser();
                        try {
                            String sql="select * from userinfo where id='"+EditTextId.getText().toString()+"'";
                            searchid=mysqlUser.select(sql);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                register();
                id=EditTextId.getText().toString();
                pwd=EditTextPassword.getText().toString();
                pwd2=EditTextPassword2.getText().toString();
                ph=EditTextPhone.getText().toString();
                idnum=EditTextIdNumber.getText().toString();
                if(searchid==null&&ph.length()==11&&idnum.length()==18&&id.length()>=6&&id.length()<=12&&pwd.length()>=6&&pwd.length()<=16){
                    Thread thread1=new Thread(new Runnable() {
                        @Override
                        public void run() {
                            MysqlUser mysqlUser=new MysqlUser();
                            try {
                                String str="0";
                                String sql="insert into userinfo(id,password,phone,idnumber,power) values('"+EditTextId.getText().toString()+"','"+EditTextPassword.getText().toString()+"','"+EditTextPhone.getText().toString()+"','"+EditTextIdNumber.getText().toString()+"','"+str+"')";
                                mysqlUser.insert(sql);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    thread1.start();
                    try {
                        thread1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                Message msg=new Message();
                msg.arg1=event;
                msg.arg2=result;
                msg.obj=data;
                handler.sendMessage(msg);
            }
        };
        SMSSDK.registerEventHandler(eventHandler);
    }

    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }

    Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int event=msg.arg1;
            int result=msg.arg2;
            Object data=msg.obj;
            if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                if(result == SMSSDK.RESULT_COMPLETE) {
                    boolean smart = (Boolean)data;
                    if(smart) {
                        Toast.makeText(getApplicationContext(),"该手机号已经注册过,请重新输入",
                                Toast.LENGTH_LONG).show();
                        EditTextPhone.requestFocus();
                        return;
                    }
                }
            }
            if(result==SMSSDK.RESULT_COMPLETE)
            {

                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    Toast.makeText(getApplicationContext(), "验证码输入正确",
                            Toast.LENGTH_LONG).show();
                }
            }
            else
            {
                if(flag)
                {
                    ButtonGetcode.setVisibility(View.VISIBLE);
                    Toast.makeText(getApplicationContext(),"验证码获取失败请重新获取", Toast.LENGTH_LONG).show();
                    EditTextPhone.requestFocus();
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"验证码输入错误", Toast.LENGTH_LONG).show();
                }
            }

        }

    };
    private boolean judPhone()
    {
        if(TextUtils.isEmpty(EditTextPhone.getText().toString().trim()))
        {
            Toast.makeText(RegisterActivity.this,"请输入您的电话号码",Toast.LENGTH_LONG).show();
            EditTextPhone.requestFocus();
            return false;
        }
        else if(EditTextPhone.getText().toString().trim().length()!=11)
        {
            Toast.makeText(RegisterActivity.this,"您的电话号码位数不正确",Toast.LENGTH_LONG).show();
            EditTextPhone.requestFocus();
            return false;
        }
        else
        {
            phone_number=EditTextPhone.getText().toString().trim();
            String num="[1][358]\\d{9}";
            if(phone_number.matches(num))
                return true;
            else
            {
                Toast.makeText(RegisterActivity.this,"请输入正确的手机号码",Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }

    private boolean judCord()
    {
        judPhone();
        if(TextUtils.isEmpty(EditTextCode.getText().toString().trim()))
        {
            Toast.makeText(RegisterActivity.this,"请输入您的验证码",Toast.LENGTH_LONG).show();
            EditTextCode.requestFocus();
            return false;
        }
        else if(EditTextCode.getText().toString().trim().length()!=4)
        {
            Toast.makeText(RegisterActivity.this,"您的验证码位数不正确",Toast.LENGTH_LONG).show();
            EditTextCode.requestFocus();

            return false;
        }
        else
        {
            cord_number=EditTextCode.getText().toString().trim();
            return true;
        }

    }

    private void register(){
        id=EditTextId.getText().toString();
        pwd=EditTextPassword.getText().toString();
        pwd2=EditTextPassword2.getText().toString();
        ph=EditTextPhone.getText().toString();
        idnum=EditTextIdNumber.getText().toString();
        if (searchid!=null){
            Toast.makeText(this,"id重复",Toast.LENGTH_LONG).show();
            return;
        }
        if(id.length()<1){
            Toast.makeText(this,"id不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if(id.length()<6||id.length()>12){
            Toast.makeText(this,"id长度不合要求",Toast.LENGTH_LONG).show();
            return;
        }
        if(pwd.length()<1){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if(pwd.length()<6||pwd.length()>16){
            Toast.makeText(this,"密码长度不合要求",Toast.LENGTH_LONG).show();
            return;
        }
        if(!pwd.equals(pwd2)){
            Toast.makeText(this,"两次密码不相同",Toast.LENGTH_LONG).show();
            return;
        }
        if(ph.length()<1){
            Toast.makeText(this,"手机号不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if(ph.length()!=11){
            Toast.makeText(this,"手机号长度不合要求",Toast.LENGTH_LONG).show();
            return;
        }
        if(judCord()){
            SMSSDK.submitVerificationCode("86",phone_number,cord_number);
            flag=false;
        }
        if(idnum.length()<1){
            Toast.makeText(this,"身份证号不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if(idnum.length()!=18){
            Toast.makeText(this,"身份证号长度不合要求",Toast.LENGTH_LONG).show();
            return;
        }
        Toast.makeText(this,"注册成功",Toast.LENGTH_LONG).show();

        SharedPreferences sp=this.getSharedPreferences("user",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("id",id);
        editor.putString("pwd",pwd);
        editor.putString("pwd2",pwd2);
        editor.putString("ph",ph);
        editor.putString("idnum",idnum);
        editor.apply();
        finish();
        Intent intent=new Intent(RegisterActivity.this, MainActivity.class);
        startActivity(intent);
    }
}

 

posted @ 2021-05-06 19:00  张笑天  阅读(20)  评论(0编辑  收藏  举报