注册功能的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 }