注册功能的布局文件
activity_register.xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.justloginregistertest.RegisterActivity" android:background="#eeeeee" > <RelativeLayout android:id="@+id/rl_registeractivity_top" android:layout_width="match_parent" android:layout_height="70dp" android:background="@color/color_minefragment_top" > <ImageView android:id="@+id/iv_registeractivity_back" android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/ic_left_back" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:clickable="true" android:onClick="onClick" /> <TextView android:id="@+id/tv_registeractivity_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textColor="#fff" android:textSize="20dp" android:layout_toRightOf="@+id/iv_registeractivity_back" android:layout_centerVertical="true" android:layout_marginLeft="20dp" /> </RelativeLayout> <LinearLayout android:id="@+id/ll_registeractivity_body" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@+id/rl_registeractivity_top" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" > <!-- 第一个文本编辑框 输入用户名 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_registeractivity_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:"/> <EditText android:id="@+id/et_registeractivity_username" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入用户名" android:gravity="center_vertical" android:layout_marginLeft="10dp" /> </LinearLayout> <!-- 第二个文本编辑框 输入密码 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_registeractivity_password1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:"/> <EditText android:id="@+id/et_registeractivity_password1" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:layout_marginLeft="10dp" android:inputType="textPassword" android:hint="请输入密码" /> </LinearLayout> <!-- 第三个文本编辑框 再次输入密码 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_registeractivity_password2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:"/> <EditText android:id="@+id/et_registeractivity_password2" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请再次输入密码" android:gravity="center_vertical" android:layout_marginLeft="10dp" android:inputType="textPassword" /> </LinearLayout> <!-- 验证码部分 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:layout_marginTop="10dp" android:orientation="horizontal" > <!-- 第四个文本编辑框 输入验证码 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_registeractivity_phoneCodes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证码:"/> <EditText android:id="@+id/et_registeractivity_phoneCodes" android:layout_width="0dp" android:layout_weight="3" android:layout_height="50dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:hint="请输入4位验证码" /> <ImageView android:id="@+id/iv_registeractivity_showCode" android:layout_width="0dp" android:layout_height="70dp" android:layout_weight="1.5" android:clickable="true" android:onClick="onClick" android:layout_marginTop="-10dp" /> </LinearLayout> </LinearLayout> <!-- 注册按钮 --> <Button android:id="@+id/bt_registeractivity_register" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="5dp" android:background="@drawable/selector_loginactivity_button" android:textColor="#fff" android:text="注册" android:onClick="onClick" android:layout_marginTop="40dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/rl_registeractivity_bottom" android:layout_marginTop="10dp" > <TextView android:layout_centerInParent="true" android:text="注册即代表同意《用户协议》" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> </LinearLayout> </RelativeLayout>
界面效果:
注册功能的RegisterActivity文件:
1 package com.example.justloginregistertest; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.text.TextUtils; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.ImageView; 10 import android.widget.LinearLayout; 11 import android.widget.RelativeLayout; 12 import android.widget.Toast; 13 14 import androidx.appcompat.app.AppCompatActivity; 15 /** 16 * Created by littlecurl 2018/6/24 17 */ 18 /** 19 * 此类 implements View.OnClickListener 之后, 20 * 就可以把onClick事件写到onCreate()方法之外 21 * 这样,onCreate()方法中的代码就不会显得很冗余 22 */ 23 public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { 24 25 private String realCode; 26 private DBOpenHelper mDBOpenHelper; 27 private Button mBtRegisteractivityRegister; 28 private RelativeLayout mRlRegisteractivityTop; 29 private ImageView mIvRegisteractivityBack; 30 private LinearLayout mLlRegisteractivityBody; 31 private EditText mEtRegisteractivityUsername; 32 private EditText mEtRegisteractivityPassword1; 33 private EditText mEtRegisteractivityPassword2; 34 private EditText mEtRegisteractivityPhonecodes; 35 private ImageView mIvRegisteractivityShowcode; 36 private RelativeLayout mRlRegisteractivityBottom; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_register); 42 43 initView(); 44 45 mDBOpenHelper = new DBOpenHelper(this); 46 47 //将验证码用图片的形式显示出来 48 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); 49 realCode = Code.getInstance().getCode().toLowerCase(); 50 } 51 52 private void initView(){ 53 mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register); 54 mRlRegisteractivityTop = findViewById(R.id.rl_registeractivity_top); 55 mIvRegisteractivityBack = findViewById(R.id.iv_registeractivity_back); 56 mLlRegisteractivityBody = findViewById(R.id.ll_registeractivity_body); 57 mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_username); 58 mEtRegisteractivityPassword1 = findViewById(R.id.et_registeractivity_password1); 59 mEtRegisteractivityPassword2 = findViewById(R.id.et_registeractivity_password2); 60 mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes); 61 mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode); 62 mRlRegisteractivityBottom = findViewById(R.id.rl_registeractivity_bottom); 63 64 /** 65 * 注册页面能点击的就三个地方 66 * top处返回箭头、刷新验证码图片、注册按钮 67 */ 68 mIvRegisteractivityBack.setOnClickListener(this); 69 mIvRegisteractivityShowcode.setOnClickListener(this); 70 mBtRegisteractivityRegister.setOnClickListener(this); 71 } 72 73 public void onClick(View view) { 74 switch (view.getId()) { 75 case R.id.iv_registeractivity_back: //返回登录页面 76 Intent intent1 = new Intent(this, loginActivity.class); 77 startActivity(intent1); 78 finish(); 79 break; 80 case R.id.iv_registeractivity_showCode: //改变随机验证码的生成 81 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); 82 realCode = Code.getInstance().getCode().toLowerCase(); 83 break; 84 case R.id.bt_registeractivity_register: //注册按钮 85 //获取用户输入的用户名、密码、验证码 86 String username = mEtRegisteractivityUsername.getText().toString().trim(); 87 String password = mEtRegisteractivityPassword2.getText().toString().trim(); 88 String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase(); 89 //注册验证 90 if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(phoneCode) ) { 91 if (phoneCode.equals(realCode)) { 92 //将用户名和密码加入到数据库中 93 mDBOpenHelper.add(username, password); 94 Intent intent2 = new Intent(this, MainActivity.class); 95 startActivity(intent2); 96 finish(); 97 Toast.makeText(this, "验证通过,注册成功", Toast.LENGTH_SHORT).show(); 98 } else { 99 Toast.makeText(this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show(); 100 } 101 }else { 102 Toast.makeText(this, "未完善信息,注册失败", Toast.LENGTH_SHORT).show(); 103 } 104 break; 105 } 106 } 107 }
注册功能需要随机生成二维码
生成二维码的Code文件:
1 package com.example.justloginregistertest; 2 3 import android.graphics.Bitmap; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 8 import java.util.Random; 9 10 /** 11 * Created by littlecurl 2018/6/24 12 */ 13 14 public class Code { 15 /** 16 * 随机数数组 17 * 去除了易混淆的 数字 0 和 字母 o O 18 * 数字 1 和 字母 i I l L 19 * 数字 6 和 字母 b 20 * 数字 9 和 字母 q 21 * 字母 c C 和 G 22 * 字母 t (经常和随机线混在一起看不清) 23 */ 24 private static final char[] CHARS = { 25 '2', '3', '4', '5', '7', '8', 26 'a', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 27 'n', 'p', 'r', 's', 'u', 'v', 'w', 'x', 'y', 'z', 28 'A', 'B', 'D', 'E', 'F', 'H', 'J', 'K', 'M', 29 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' 30 }; 31 32 private static Code bmpCode; 33 34 public static Code getInstance() { 35 if(bmpCode == null) 36 bmpCode = new Code(); 37 return bmpCode; 38 } 39 40 //default settings 41 //验证码默认随机数的个数 42 private static final int DEFAULT_CODE_LENGTH = 4; 43 //默认字体大小 44 private static final int DEFAULT_FONT_SIZE = 25; 45 //默认线条的条数 46 private static final int DEFAULT_LINE_NUMBER = 5; 47 //padding值 48 private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20; 49 //验证码的默认宽高 50 private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40; 51 52 //settings decided by the layout xml 53 //canvas width and height 54 private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; 55 56 //random word space and pading_top 57 private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT, 58 base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP; 59 60 //number of chars, lines; font size 61 private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE; 62 63 //variables 64 private String code; 65 private int padding_left, padding_top; 66 private Random random = new Random(); 67 //验证码图片 68 public Bitmap createBitmap() { 69 padding_left = 0; 70 71 Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 72 Canvas c = new Canvas(bp); 73 74 code = createCode(); 75 76 c.drawColor(Color.WHITE); 77 Paint paint = new Paint(); 78 paint.setAntiAlias(true); 79 paint.setTextSize(font_size); 80 //画验证码 81 for (int i = 0; i < code.length(); i++) { 82 randomTextStyle(paint); 83 randomPadding(); 84 c.drawText(code.charAt(i) + "", padding_left, padding_top, paint); 85 } 86 //画线条 87 for (int i = 0; i < line_number; i++) { 88 drawLine(c, paint); 89 } 90 91 // c.save( Canvas.ALL_SAVE_FLAG );//保存 92 c.save();//保存 93 c.restore();// 94 return bp; 95 } 96 97 public String getCode() { 98 return code; 99 } 100 101 //生成验证码 102 private String createCode() { 103 StringBuilder buffer = new StringBuilder(); 104 for (int i = 0; i < codeLength; i++) { 105 buffer.append(CHARS[random.nextInt(CHARS.length)]); 106 } 107 return buffer.toString(); 108 } 109 //画干扰线 110 private void drawLine(Canvas canvas, Paint paint) { 111 int color = randomColor(); 112 int startX = random.nextInt(width); 113 int startY = random.nextInt(height); 114 int stopX = random.nextInt(width); 115 int stopY = random.nextInt(height); 116 paint.setStrokeWidth(1); 117 paint.setColor(color); 118 canvas.drawLine(startX, startY, stopX, stopY, paint); 119 } 120 //生成随机颜色 121 private int randomColor() { 122 return randomColor(1); 123 } 124 125 private int randomColor(int rate) { 126 int red = random.nextInt(256) / rate; 127 int green = random.nextInt(256) / rate; 128 int blue = random.nextInt(256) / rate; 129 return Color.rgb(red, green, blue); 130 } 131 //随机生成文字样式,颜色,粗细,倾斜度 132 private void randomTextStyle(Paint paint) { 133 int color = randomColor(); 134 paint.setColor(color); 135 paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体 136 float skewX = random.nextInt(11) / 10; 137 skewX = random.nextBoolean() ? skewX : -skewX; 138 paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜 139 //paint.setUnderlineText(true); //true为下划线,false为非下划线 140 //paint.setStrikeThruText(true); //true为删除线,false为非删除线 141 } 142 //随机生成padding值 143 private void randomPadding() { 144 padding_left += base_padding_left + random.nextInt(range_padding_left); 145 padding_top = base_padding_top + random.nextInt(range_padding_top); 146 } 147 }
注册功能的RegisterActivity文件:
1 package com.example.justloginregistertest; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.text.TextUtils; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.ImageView; 10 import android.widget.LinearLayout; 11 import android.widget.RelativeLayout; 12 import android.widget.Toast; 13 14 import androidx.appcompat.app.AppCompatActivity; 15 /** 16 * Created by littlecurl 2018/6/24 17 */ 18 /** 19 * 此类 implements View.OnClickListener 之后, 20 * 就可以把onClick事件写到onCreate()方法之外 21 * 这样,onCreate()方法中的代码就不会显得很冗余 22 */ 23 public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { 24 25 private String realCode; 26 private DBOpenHelper mDBOpenHelper; 27 private Button mBtRegisteractivityRegister; 28 private RelativeLayout mRlRegisteractivityTop; 29 private ImageView mIvRegisteractivityBack; 30 private LinearLayout mLlRegisteractivityBody; 31 private EditText mEtRegisteractivityUsername; 32 private EditText mEtRegisteractivityPassword1; 33 private EditText mEtRegisteractivityPassword2; 34 private EditText mEtRegisteractivityPhonecodes; 35 private ImageView mIvRegisteractivityShowcode; 36 private RelativeLayout mRlRegisteractivityBottom; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_register); 42 43 initView(); 44 45 mDBOpenHelper = new DBOpenHelper(this); 46 47 //将验证码用图片的形式显示出来 48 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); 49 realCode = Code.getInstance().getCode().toLowerCase(); 50 } 51 52 private void initView(){ 53 mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register); 54 mRlRegisteractivityTop = findViewById(R.id.rl_registeractivity_top); 55 mIvRegisteractivityBack = findViewById(R.id.iv_registeractivity_back); 56 mLlRegisteractivityBody = findViewById(R.id.ll_registeractivity_body); 57 mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_username); 58 mEtRegisteractivityPassword1 = findViewById(R.id.et_registeractivity_password1); 59 mEtRegisteractivityPassword2 = findViewById(R.id.et_registeractivity_password2); 60 mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes); 61 mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode); 62 mRlRegisteractivityBottom = findViewById(R.id.rl_registeractivity_bottom); 63 64 /** 65 * 注册页面能点击的就三个地方 66 * top处返回箭头、刷新验证码图片、注册按钮 67 */ 68 mIvRegisteractivityBack.setOnClickListener(this); 69 mIvRegisteractivityShowcode.setOnClickListener(this); 70 mBtRegisteractivityRegister.setOnClickListener(this); 71 } 72 73 public void onClick(View view) { 74 switch (view.getId()) { 75 case R.id.iv_registeractivity_back: //返回登录页面 76 Intent intent1 = new Intent(this, loginActivity.class); 77 startActivity(intent1); 78 finish(); 79 break; 80 case R.id.iv_registeractivity_showCode: //改变随机验证码的生成 81 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); 82 realCode = Code.getInstance().getCode().toLowerCase(); 83 break; 84 case R.id.bt_registeractivity_register: //注册按钮 85 //获取用户输入的用户名、密码、验证码 86 String username = mEtRegisteractivityUsername.getText().toString().trim(); 87 String password = mEtRegisteractivityPassword2.getText().toString().trim(); 88 String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase(); 89 //注册验证 90 if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(phoneCode) ) { 91 if (phoneCode.equals(realCode)) { 92 //将用户名和密码加入到数据库中 93 mDBOpenHelper.add(username, password); 94 Intent intent2 = new Intent(this, MainActivity.class); 95 startActivity(intent2); 96 finish(); 97 Toast.makeText(this, "验证通过,注册成功", Toast.LENGTH_SHORT).show(); 98 } else { 99 Toast.makeText(this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show(); 100 } 101 }else { 102 Toast.makeText(this, "未完善信息,注册失败", Toast.LENGTH_SHORT).show(); 103 } 104 break; 105 } 106 } 107 }