登录页面练习
主页面
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <RadioGroup 8 android:id="@+id/rg_login" 9 android:layout_width="match_parent" 10 android:layout_height="@dimen/item_layout_height" 11 android:orientation="horizontal"> 12 13 <RadioButton 14 android:id="@+id/rb_password" 15 android:layout_width="0dp" 16 android:layout_height="match_parent" 17 android:layout_weight="1" 18 android:checked="true" 19 android:text="@string/login_by_password" 20 android:textSize="@dimen/common_font_size" 21 android:textColor="@color/black"/> 22 23 <RadioButton 24 android:id="@+id/rb_verifycode" 25 android:layout_width="0dp" 26 android:layout_height="match_parent" 27 android:layout_weight="1" 28 android:text="@string/login_by_verifycode" 29 android:textSize="@dimen/common_font_size" 30 android:textColor="@color/black"/> 31 32 </RadioGroup> 33 <LinearLayout 34 android:layout_width="match_parent" 35 android:layout_height="@dimen/item_layout_height" 36 android:orientation="horizontal"> 37 38 <TextView 39 android:layout_width="wrap_content" 40 android:layout_height="match_parent" 41 android:gravity="center" 42 android:text="@string/phone_number" 43 android:textColor="@color/black" 44 android:textSize="@dimen/common_font_size"/> 45 46 <EditText 47 android:id="@+id/et_phone" 48 android:layout_width="0dp" 49 android:layout_height="match_parent" 50 android:layout_weight="1" 51 android:layout_marginTop="5dp" 52 android:layout_marginBottom="5dp" 53 android:background="@drawable/editext_selector" 54 android:hint="@string/input_phone_number" 55 android:inputType="number" 56 android:maxLength="11" 57 android:textColor="@color/black" 58 android:textColorHint="@color/grey" 59 android:textSize="@dimen/common_font_size"/> 60 61 </LinearLayout> 62 63 <LinearLayout 64 android:layout_width="match_parent" 65 android:layout_height="@dimen/item_layout_height" 66 android:orientation="horizontal"> 67 68 <TextView 69 android:id="@+id/tv_password" 70 android:layout_width="wrap_content" 71 android:layout_height="match_parent" 72 android:gravity="center" 73 android:text="@string/login_password" 74 android:textColor="@color/black" 75 android:textSize="@dimen/common_font_size"/> 76 77 <RelativeLayout 78 android:layout_width="0dp" 79 android:layout_height="match_parent" 80 android:layout_weight="1"> 81 82 <EditText 83 android:id="@+id/et_password" 84 android:layout_width="match_parent" 85 android:layout_height="match_parent" 86 android:layout_weight="1" 87 android:layout_marginTop="5dp" 88 android:layout_marginBottom="5dp" 89 android:background="@drawable/editext_selector" 90 android:hint="@string/input_password" 91 android:inputType="numberPassword" 92 android:maxLength="11" 93 android:textColor="@color/black" 94 android:textColorHint="@color/grey" 95 android:textSize="@dimen/common_font_size"/> 96 97 <Button 98 android:id="@+id/btn_forget" 99 android:layout_width="wrap_content" 100 android:layout_height="match_parent" 101 android:layout_alignParentEnd="true" 102 android:text="@string/forget_password" 103 android:textColor="@color/black" 104 android:textSize="@dimen/common_font_size"/> 105 106 </RelativeLayout> 107 108 </LinearLayout> 109 110 <CheckBox 111 android:id="@+id/ck_remember" 112 android:layout_width="match_parent" 113 android:layout_height="wrap_content" 114 android:button="@drawable/checkbox_selector" 115 android:text="@string/remember_password" 116 android:textColor="@color/black" 117 android:textSize="@dimen/common_font_size" /> 118 119 <Button 120 android:id="@+id/btn_login" 121 android:layout_width="match_parent" 122 android:layout_height="wrap_content" 123 android:text="@string/login" 124 android:textColor="@color/black" 125 android:textSize="@dimen/button_font_size" /> 126 </LinearLayout>
主功能
1 package com.example.login_text; 2 3 import androidx.activity.result.ActivityResult; 4 import androidx.activity.result.ActivityResultCallback; 5 import androidx.activity.result.ActivityResultLauncher; 6 import androidx.activity.result.contract.ActivityResultContract; 7 import androidx.activity.result.contract.ActivityResultContracts; 8 import androidx.appcompat.app.AppCompatActivity; 9 10 import android.app.Activity; 11 import android.app.AlertDialog; 12 import android.content.Intent; 13 import android.os.Bundle; 14 import android.text.Editable; 15 import android.text.TextWatcher; 16 import android.view.View; 17 import android.widget.Button; 18 import android.widget.CheckBox; 19 import android.widget.EditText; 20 import android.widget.RadioButton; 21 import android.widget.RadioGroup; 22 import android.widget.TextView; 23 import android.widget.Toast; 24 25 import com.example.login_text.util.ViewUtil; 26 27 import java.util.Random; 28 29 public class LoginMainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { 30 31 private TextView tv_password; 32 private EditText et_password; 33 private Button btn_forget; 34 private CheckBox ck_remember; 35 private RadioButton rb_password; 36 private RadioButton rb_verifycode; 37 private EditText et_phone; 38 private Button btn_login; 39 private ActivityResultLauncher<Intent> register; 40 private String mPassword = "111111"; 41 private String mVerifyCode; 42 43 @Override 44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.activity_login_main); 47 RadioGroup rb_login = findViewById(R.id.rg_login); 48 tv_password = findViewById(R.id.tv_password); 49 et_phone = findViewById(R.id.et_phone); 50 et_password = findViewById(R.id.et_password); 51 btn_forget = findViewById(R.id.btn_forget); 52 ck_remember = findViewById(R.id.ck_remember); 53 rb_password = findViewById(R.id.rb_password); 54 rb_verifycode = findViewById(R.id.rb_verifycode); 55 btn_login = findViewById(R.id.btn_login); 56 // 给rg_login设置单选监听器 57 rb_login.setOnCheckedChangeListener(this); 58 // 给et_phone添加文本变更监听器 59 et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11)); 60 // 给et_password添加文本变更监听器 61 et_password.addTextChangedListener(new HideTextWatcher(et_password, 6)); 62 btn_forget.setOnClickListener(this); 63 btn_login.setOnClickListener(this); 64 rb_login.setOnCheckedChangeListener(this); 65 66 register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { 67 68 @Override 69 public void onActivityResult(ActivityResult result) { 70 Intent intent = result.getData(); 71 if(intent != null && result.getResultCode() == Activity.RESULT_OK) 72 { 73 // 用户密码已改为新密码,故更新密码变量 74 mPassword = intent.getStringExtra("new_password"); 75 } 76 } 77 }); 78 } 79 80 @Override 81 public void onCheckedChanged(RadioGroup group, int checkedId) { 82 switch(checkedId){ 83 case R.id.rb_password: 84 tv_password.setText(getString(R.string.login_password)); 85 et_password.setHint(getString(R.string.input_password)); 86 btn_forget.setText(getString(R.string.forget_password)); 87 ck_remember.setVisibility(View.VISIBLE); 88 break; 89 case R.id.rb_verifycode: 90 tv_password.setText(getString(R.string.verifycode)); 91 et_password.setHint(getString(R.string.input_verifycode)); 92 btn_forget.setText(getString(R.string.get_verifycode)); 93 ck_remember.setVisibility(View.GONE); 94 break; 95 } 96 97 } 98 99 @Override 100 public void onClick(View v ) { 101 102 String phone = et_phone.getText().toString(); 103 if (phone.length() < 11) { 104 Toast.makeText(this, "手机号位数错误", Toast.LENGTH_SHORT).show(); 105 return; 106 } 107 switch (v.getId()) { 108 case R.id.btn_forget: 109 if (rb_password.isChecked()) { 110 // 选择了密码方式校验,此时要跳到找回密码页面 111 Intent intent = new Intent(this, LoginForgetActivity.class); 112 intent.putExtra("phone", phone); 113 register.launch(intent); 114 } else if (rb_verifycode.isClickable()) { 115 // 生成六位随机数字的验证码 116 mVerifyCode = String.format("%06d", new Random().nextInt(999999)); 117 // 以下弹出提醒对话框,提示用户记住六位验证码数字 118 AlertDialog.Builder builder = new AlertDialog.Builder(this); 119 builder.setTitle("请记住验证码"); 120 builder.setMessage("手机号" + phone + ",本次验证码是" + mVerifyCode + ",请输入验证码"); 121 builder.setPositiveButton("好的", null); 122 AlertDialog dialog = builder.create(); 123 dialog.show(); 124 } 125 break; 126 case R.id.btn_login: 127 // 密码方式校验 128 if (rb_password.isChecked()) { 129 if (!mPassword.equals(et_password.getText().toString())) { 130 Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show(); 131 return; 132 } 133 // 提示用户登录成功 134 loginSuccess(); 135 } else if (rb_verifycode.isClickable()) { 136 if (!mVerifyCode.equals(et_password.getText().toString())) { 137 Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show(); 138 return; 139 } 140 // 提示用户登录成功 141 loginSuccess(); 142 } 143 break; 144 } 145 } 146 147 private void loginSuccess() { 148 String desc = String.format("您的手机号码是%s,恭喜你通过登录验证,点击“确定”按钮返回上个页面", 149 et_phone.getText().toString()); 150 // 以下弹出提醒对话框,提示用户登录成功 151 AlertDialog.Builder builder = new AlertDialog.Builder(this); 152 builder.setTitle("登录成功"); 153 builder.setMessage(desc); 154 builder.setPositiveButton("确定返回", (dialog, which) -> { 155 // 结束当前的活动页面 156 finish(); 157 }); 158 builder.setNegativeButton("我再看看", null); 159 AlertDialog dialog = builder.create(); 160 dialog.show(); 161 } 162 163 // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法 164 private class HideTextWatcher implements TextWatcher { 165 private EditText mView; 166 private int mMaxLength; 167 168 public HideTextWatcher(EditText v, int maxLength) { 169 this.mView = v; 170 this.mMaxLength = maxLength; 171 } 172 173 @Override 174 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 175 176 } 177 178 @Override 179 public void onTextChanged(CharSequence s, int start, int before, int count) { 180 181 } 182 183 @Override 184 public void afterTextChanged(Editable s) { 185 if (s.toString().length() == mMaxLength) { 186 // 隐藏输入法软键盘 187 ViewUtil.hideOneInputMethod(LoginMainActivity.this, mView); 188 } 189 } 190 } 191 }
忘记密码页面xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="@dimen/item_layout_height" 10 android:orientation="horizontal"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="match_parent" 15 android:gravity="center" 16 android:text="@string/input_new_password" 17 android:textColor="@color/black" 18 android:textSize="@dimen/common_font_size"/> 19 20 <EditText 21 android:id="@+id/et_password_first" 22 android:layout_width="0dp" 23 android:layout_height="match_parent" 24 android:layout_weight="1" 25 android:layout_marginTop="5dp" 26 android:layout_marginBottom="5dp" 27 android:background="@drawable/editext_selector" 28 android:hint="@string/input_new_password_hint" 29 android:inputType="number" 30 android:maxLength="11" 31 android:textColor="@color/black" 32 android:textColorHint="@color/grey" 33 android:textSize="@dimen/common_font_size"/> 34 35 </LinearLayout> 36 37 <LinearLayout 38 android:layout_width="match_parent" 39 android:layout_height="@dimen/item_layout_height" 40 android:orientation="horizontal"> 41 42 <TextView 43 android:layout_width="wrap_content" 44 android:layout_height="match_parent" 45 android:gravity="center" 46 android:text="@string/confirm_new_password" 47 android:textColor="@color/black" 48 android:textSize="@dimen/common_font_size"/> 49 50 <EditText 51 android:id="@+id/et_password_second" 52 android:layout_width="0dp" 53 android:layout_height="match_parent" 54 android:layout_weight="1" 55 android:layout_marginTop="5dp" 56 android:layout_marginBottom="5dp" 57 android:background="@drawable/editext_selector" 58 android:hint="@string/input_new_password_again" 59 android:inputType="number" 60 android:maxLength="11" 61 android:textColor="@color/black" 62 android:textColorHint="@color/grey" 63 android:textSize="@dimen/common_font_size"/> 64 </LinearLayout> 65 <LinearLayout 66 android:layout_width="match_parent" 67 android:layout_height="@dimen/item_layout_height" 68 android:orientation="horizontal"> 69 70 <TextView 71 android:layout_width="wrap_content" 72 android:layout_height="match_parent" 73 android:gravity="center" 74 android:text="@string/verifycode2" 75 android:textColor="@color/black" 76 android:textSize="@dimen/common_font_size"/> 77 78 <RelativeLayout 79 android:layout_width="0dp" 80 android:layout_height="match_parent" 81 android:layout_weight="1"> 82 83 <EditText 84 android:id="@+id/et_verifycode" 85 android:layout_width="match_parent" 86 android:layout_height="match_parent" 87 android:layout_weight="1" 88 android:layout_marginTop="5dp" 89 android:layout_marginBottom="5dp" 90 android:background="@drawable/editext_selector" 91 android:hint="@string/input_verifycode" 92 android:inputType="number" 93 android:maxLength="11" 94 android:textColor="@color/black" 95 android:textColorHint="@color/grey" 96 android:textSize="@dimen/common_font_size"/> 97 98 <Button 99 android:id="@+id/btn_verifycode" 100 android:layout_width="wrap_content" 101 android:layout_height="match_parent" 102 android:layout_alignParentEnd="true" 103 android:text="@string/get_verifycode" 104 android:textColor="@color/black" 105 android:textSize="@dimen/common_font_size"/> 106 107 </RelativeLayout> 108 109 </LinearLayout> 110 111 <Button 112 android:id="@+id/btn_confirm" 113 android:layout_width="match_parent" 114 android:layout_height="wrap_content" 115 android:text="@string/done" 116 android:textColor="@color/black" 117 android:textSize="@dimen/button_font_size" /> 118 </LinearLayout>
忘记密码功能java
1 package com.example.login_text; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.EditText; 11 import android.widget.Toast; 12 13 import java.util.Random; 14 15 public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener { 16 17 private String mPhone; 18 private String mVerifyCode; 19 private EditText et_password_first; 20 private EditText et_password_second; 21 private EditText et_verifycode; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_login_forget); 26 et_password_first = findViewById(R.id.et_password_first); 27 et_password_second = findViewById(R.id.et_password_second); 28 et_verifycode = findViewById(R.id.et_verifycode); 29 // 从上一个页面获取要修改密码的手机号码 30 mPhone = getIntent().getStringExtra("phone"); 31 32 findViewById(R.id.btn_verifycode).setOnClickListener(this); 33 findViewById(R.id.btn_confirm).setOnClickListener(this); 34 } 35 36 @Override 37 public void onClick(View v) { 38 switch (v.getId()){ 39 case R.id.btn_verifycode: 40 // 生成六位随机数字的验证码 41 mVerifyCode = String.format("%06d", new Random().nextInt(999999)); 42 // 以下弹出提醒对话框,提示用户记住六位验证码数字 43 AlertDialog.Builder builder = new AlertDialog.Builder(this); 44 builder.setTitle("请记住验证码"); 45 builder.setMessage("手机号" + mPhone + ",本次验证码是" + mVerifyCode + ",请输入验证码"); 46 builder.setPositiveButton("好的", null); 47 AlertDialog dialog = builder.create(); 48 dialog.show(); 49 break; 50 case R.id.btn_confirm: 51 // 点击了“确定”按钮 52 String password_first = et_password_first.getText().toString(); 53 String password_second = et_password_second.getText().toString(); 54 if(password_first.length() < 6) 55 { 56 Toast.makeText(this, "位数错误", Toast.LENGTH_SHORT).show(); 57 return; 58 } 59 if(!password_first.equals(password_second)) 60 { 61 Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show(); 62 return; 63 } 64 Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show(); 65 // 以下把修改好的新密码返回给上一个页面 66 Intent intent = new Intent(); 67 intent.putExtra("new_password",password_first); 68 setResult(Activity.RESULT_OK , intent); 69 finish(); 70 break; 71 } 72 } 73 }