基于Android Studio实现用户登录界面(含完备源码)

打印 上一主题 下一主题

主题 522|帖子 522|积分 1566

在开发Android应用时,登录界面是用户与应用交互的第一步,设计一个既安全又用户友好的登录界面至关紧张。本文将基于Android Studio实现一个包罗暗码登录、验证码登录、获取验证码、找回暗码以及记住暗码功能的登录界面。

  
1. 预备工作

1.1 环境搭建

确保你的开发环境已经安装了Android Studio,并设置了相应的Android SDK。
1.2 项目创建

在Android Studio中创建一个新的Android项目,选择Empty Activity作为起始模板。
2. 设计登录界面

   values和drawable中的设置文件按需求自定义即可,本文不一一粘贴了,如有需求欢迎私信
  2.1 登录界面XML布局

在res/layout/activity_main.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.     <RadioGroup
  7.         android:id="@+id/rg_login"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="@dimen/item_layout_height"
  10.         android:orientation="horizontal">
  11.         <RadioButton
  12.             android:id="@+id/rb_password"
  13.             android:layout_width="0dp"
  14.             android:layout_height="match_parent"
  15.             android:layout_weight="1"
  16.             android:checked="true"
  17.             android:text="@string/login_by_password"
  18.             android:textColor="@color/black"
  19.             android:textSize="@dimen/common_font_size" />
  20.         <RadioButton
  21.             android:id="@+id/rb_verifycode"
  22.             android:layout_width="0dp"
  23.             android:layout_height="match_parent"
  24.             android:layout_weight="1"
  25.             android:text="@string/login_by_verifycode"
  26.             android:textColor="@color/black"
  27.             android:textSize="@dimen/common_font_size" />
  28.     </RadioGroup>
  29.     <LinearLayout
  30.         android:layout_width="match_parent"
  31.         android:layout_height="@dimen/item_layout_height"
  32.         android:orientation="horizontal">
  33.         <TextView
  34.             android:id="@+id/tv_phone"
  35.             android:layout_width="wrap_content"
  36.             android:layout_height="match_parent"
  37.             android:gravity="center"
  38.             android:text="@string/phone_number"
  39.             android:textColor="@color/black"
  40.             android:textSize="@dimen/common_font_size" />
  41.         <EditText
  42.             android:id="@+id/et_phone"
  43.             android:layout_width="0dp"
  44.             android:layout_height="match_parent"
  45.             android:layout_marginTop="5dp"
  46.             android:layout_marginBottom="5dp"
  47.             android:layout_weight="1"
  48.             android:background="@drawable/editext_selector"
  49.             android:hint="@string/input_phone_number"
  50.             android:inputType="number"
  51.             android:maxLength="11"
  52.             android:textColor="@color/black"
  53.             android:textColorHint="@color/grey"
  54.             android:textSize="@dimen/common_font_size" />
  55.     </LinearLayout>
  56.     <LinearLayout
  57.         android:layout_width="match_parent"
  58.         android:layout_height="@dimen/item_layout_height"
  59.         android:orientation="horizontal">
  60.         <TextView
  61.             android:id="@+id/tv_password"
  62.             android:layout_width="wrap_content"
  63.             android:layout_height="match_parent"
  64.             android:gravity="center"
  65.             android:text="@string/login_password"
  66.             android:textColor="@color/black"
  67.             android:textSize="@dimen/common_font_size" />
  68.         <RelativeLayout
  69.             android:layout_width="0dp"
  70.             android:layout_height="match_parent"
  71.             android:layout_weight="1">
  72.             <EditText
  73.                 android:id="@+id/et_password"
  74.                 android:layout_width="match_parent"
  75.                 android:layout_height="match_parent"
  76.                 android:layout_marginTop="5dp"
  77.                 android:layout_marginBottom="5dp"
  78.                 android:layout_weight="1"
  79.                 android:background="@drawable/editext_selector"
  80.                 android:hint="@string/input_password"
  81.                 android:inputType="numberPassword"
  82.                 android:maxLength="11"
  83.                 android:textColor="@color/black"
  84.                 android:textColorHint="@color/grey"
  85.                 android:textSize="@dimen/common_font_size" />
  86.             <Button
  87.                 android:id="@+id/btn_forget_password"
  88.                 android:layout_width="wrap_content"
  89.                 android:layout_height="match_parent"
  90.                 android:layout_alignParentEnd="true"
  91.                 android:text="@string/forget_password"
  92.                 android:textColor="@color/black"
  93.                 android:textSize="@dimen/common_font_size" />
  94.         </RelativeLayout>
  95.     </LinearLayout>
  96.     <CheckBox
  97.         android:id="@+id/ck_remember_password"
  98.         android:layout_width="match_parent"
  99.         android:layout_height="wrap_content"
  100.         android:button="@drawable/checkbox_selector"
  101.         android:text="@string/remember_password"
  102.         android:textColor="@color/black"
  103.         android:textSize="@dimen/common_font_size" />
  104.     <Button
  105.         android:id="@+id/btn_login"
  106.         android:layout_width="match_parent"
  107.         android:layout_height="wrap_content"
  108.         android:text="@string/login"
  109.         android:textColor="@color/black"
  110.         android:textSize="@dimen/button_font_size"/>
  111. </LinearLayout>
复制代码
2.2 忘记暗码界面XML布局

在res/layout/activity_forget.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.     <LinearLayout
  7.         android:layout_width="match_parent"
  8.         android:layout_height="@dimen/item_layout_height"
  9.         android:orientation="horizontal">
  10.         <TextView
  11.             android:layout_width="wrap_content"
  12.             android:layout_height="match_parent"
  13.             android:gravity="center"
  14.             android:text="@string/input_new_password"
  15.             android:textColor="@color/black"
  16.             android:textSize="@dimen/common_font_size" />
  17.         <EditText
  18.             android:id="@+id/et_password_first"
  19.             android:layout_width="0dp"
  20.             android:layout_height="match_parent"
  21.             android:layout_marginTop="5dp"
  22.             android:layout_marginBottom="5dp"
  23.             android:layout_weight="1"
  24.             android:background="@drawable/editext_selector"
  25.             android:hint="@string/input_new_password_hint"
  26.             android:inputType="numberPassword"
  27.             android:maxLength="11"
  28.             android:textColor="@color/black"
  29.             android:textColorHint="@color/grey"
  30.             android:textSize="@dimen/common_font_size" />
  31.     </LinearLayout>
  32.     <LinearLayout
  33.         android:layout_width="match_parent"
  34.         android:layout_height="@dimen/item_layout_height"
  35.         android:orientation="horizontal">
  36.         <TextView
  37.             android:layout_width="wrap_content"
  38.             android:layout_height="match_parent"
  39.             android:gravity="center"
  40.             android:text="@string/confirm_new_password"
  41.             android:textColor="@color/black"
  42.             android:textSize="@dimen/common_font_size" />
  43.         <RelativeLayout
  44.             android:layout_width="0dp"
  45.             android:layout_height="match_parent"
  46.             android:layout_weight="1">
  47.             <EditText
  48.                 android:id="@+id/et_password_second"
  49.                 android:layout_width="match_parent"
  50.                 android:layout_height="match_parent"
  51.                 android:layout_marginTop="5dp"
  52.                 android:layout_marginBottom="5dp"
  53.                 android:layout_weight="1"
  54.                 android:background="@drawable/editext_selector"
  55.                 android:hint="@string/input_new_password_again"
  56.                 android:inputType="numberPassword"
  57.                 android:maxLength="11"
  58.                 android:textColor="@color/black"
  59.                 android:textColorHint="@color/grey"
  60.                 android:textSize="@dimen/common_font_size" />
  61.         </RelativeLayout>
  62.     </LinearLayout>
  63.     <LinearLayout
  64.         android:layout_width="match_parent"
  65.         android:layout_height="@dimen/item_layout_height"
  66.         android:orientation="horizontal">
  67.         <TextView
  68.             android:layout_width="wrap_content"
  69.             android:layout_height="match_parent"
  70.             android:gravity="center"
  71.             android:text="@string/verifycode2"
  72.             android:textColor="@color/black"
  73.             android:textSize="@dimen/common_font_size" />
  74.         <RelativeLayout
  75.             android:layout_width="0dp"
  76.             android:layout_height="match_parent"
  77.             android:layout_weight="1">
  78.             <EditText
  79.                 android:id="@+id/et_verifycode"
  80.                 android:layout_width="match_parent"
  81.                 android:layout_height="match_parent"
  82.                 android:layout_marginTop="5dp"
  83.                 android:layout_marginBottom="5dp"
  84.                 android:layout_weight="1"
  85.                 android:background="@drawable/editext_selector"
  86.                 android:hint="@string/input_verifycode"
  87.                 android:inputType="numberPassword"
  88.                 android:maxLength="11"
  89.                 android:textColor="@color/black"
  90.                 android:textColorHint="@color/grey"
  91.                 android:textSize="@dimen/common_font_size" />
  92.             <Button
  93.                 android:id="@+id/btn_verifycode"
  94.                 android:layout_width="wrap_content"
  95.                 android:layout_height="match_parent"
  96.                 android:layout_alignParentEnd="true"
  97.                 android:text="@string/get_verifycode"
  98.                 android:textColor="@color/black"
  99.                 android:textSize="@dimen/common_font_size" />
  100.         </RelativeLayout>
  101.     </LinearLayout>
  102.     <Button
  103.         android:id="@+id/btn_confirm"
  104.         android:layout_width="match_parent"
  105.         android:layout_height="wrap_content"
  106.         android:text="@string/done"
  107.         android:textColor="@color/black"
  108.         android:textSize="@dimen/button_font_size"/>
  109. </LinearLayout>
复制代码
3. 实现功能

3.1 LoginMainActivity.java

登录界面业务逻辑
  1. package com.example.login_view;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.CheckBox;
  13. import android.widget.EditText;
  14. import android.widget.RadioButton;
  15. import android.widget.RadioGroup;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import androidx.activity.result.ActivityResult;
  19. import androidx.activity.result.ActivityResultCallback;
  20. import androidx.activity.result.ActivityResultLauncher;
  21. import androidx.activity.result.contract.ActivityResultContracts;
  22. import androidx.appcompat.app.AlertDialog;
  23. import androidx.appcompat.app.AppCompatActivity;
  24. import com.example.login_view.utils.ViewUtil;
  25. import java.util.Random;
  26. public class LoginMainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
  27.     private TextView tv_password;
  28.     private EditText et_password;
  29.     private Button btn_forget;
  30.     private CheckBox ck_remember;
  31.     private EditText et_phone;
  32.     private RadioButton rb_password;
  33.     private RadioButton rb_verifycode;
  34.     private ActivityResultLauncher<Intent> register;
  35.     private Button btn_login;
  36.     private String mPassword = "111111";
  37.     private String mVerifyCode;
  38.     private SharedPreferences preferences;
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.activity_login_main);
  43.         RadioGroup rg_login = findViewById(R.id.rg_login);
  44.         tv_password = findViewById(R.id.tv_password);
  45.         et_phone = findViewById(R.id.et_phone);
  46.         et_password = findViewById(R.id.et_password);
  47.         btn_forget = findViewById(R.id.btn_forget_password);
  48.         ck_remember = findViewById(R.id.ck_remember_password);
  49.         rb_password = findViewById(R.id.rb_password);
  50.         rb_verifycode = findViewById(R.id.rb_verifycode);
  51.         btn_login = findViewById(R.id.btn_login);
  52.         //设置单选按钮的监听器
  53.         rg_login.setOnCheckedChangeListener(this);
  54.         et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
  55.         et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));
  56.         btn_forget.setOnClickListener(this);
  57.         btn_login.setOnClickListener(this);
  58.         register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
  59.             @Override
  60.             public void onActivityResult(ActivityResult result) {
  61.                 Intent intent = result.getData();
  62.                 if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
  63.                     //更新密码
  64.                     mPassword = intent.getStringExtra("new_password");
  65.                 }
  66.             }
  67.         });
  68.         preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
  69.         reload();
  70.     }
  71.     private void reload() {
  72.         boolean isRemember = preferences.getBoolean("is_remember", false);
  73.         if (isRemember) {
  74.             String phone = preferences.getString("phone", "");
  75.             String password = preferences.getString("password", "");
  76.             et_phone.setText(phone);
  77.             et_password.setText(password);
  78.             ck_remember.setChecked(true);
  79.         }
  80.     }
  81.     //单选按钮的点击事件
  82.     @Override
  83.     public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
  84.         //如果选中了密码登录
  85.         if (checkedId == R.id.rb_password) {
  86.             //设置文本
  87.             tv_password.setText(getString(R.string.login_password));
  88.             //设置输入框的提示
  89.             et_password.setHint(getString(R.string.input_password));
  90.             //设置按钮的文本
  91.             btn_forget.setText(getString(R.string.forget_password));
  92.             //隐藏记住密码的复选框
  93.             ck_remember.setVisibility(View.VISIBLE);
  94.         }
  95.         //如果选中了验证码登录
  96.         else if (checkedId == R.id.rb_verifycode) {
  97.             //设置文本
  98.             tv_password.setText(getString(R.string.verifycode));
  99.             //设置输入框的提示
  100.             et_password.setHint(getString(R.string.input_verifycode));
  101.             //设置按钮的文本
  102.             btn_forget.setText(getString(R.string.get_verifycode));
  103.             //隐藏记住密码的复选框
  104.             ck_remember.setVisibility(View.GONE);
  105.         }
  106.     }
  107.     @Override
  108.     public void onClick(View view) {
  109.         Log.d("panbug", "log");
  110.         String phone = et_phone.getText().toString();
  111.         Log.d("panbug", "log2");
  112.         //判断手机号是否为空
  113.         if (phone.length() < 11) {
  114.             Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
  115.             Log.d("panbug", "log3");
  116.             return;
  117.         }
  118.         if (view.getId() == R.id.btn_forget_password) {
  119.             //如果选中了密码登录,跳到找回密码界面
  120.             if (rb_password.isChecked()) {
  121.                 //携带手机号码跳转到找回密码界面
  122.                 Intent intent = new Intent(this, LoginForgetActivity.class);
  123.                 intent.putExtra("phone", phone);
  124.                 register.launch(intent);
  125.             } else if (rb_verifycode.isChecked()) {
  126.                 //如果选中了验证码登录,生成六位随机验证码
  127.                 mVerifyCode = String.format("%06d", new Random().nextInt(999999));
  128.                 //弹出提醒对话框,提示用户记住六位验证码
  129.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  130.                 builder.setTitle("请记住验证码");
  131.                 builder.setMessage("手机号" + phone + "的验证码是:" + mVerifyCode + ",请输入验证码");
  132.                 builder.setPositiveButton("确定", null);
  133.                 AlertDialog dialog = builder.create();
  134.                 dialog.show();
  135.             }
  136.         } else if (view.getId() == R.id.btn_login) {
  137.             //密码方式校验
  138.             if (rb_password.isChecked()) {
  139.                 if (!mPassword.equals(et_password.getText().toString())) {
  140.                     Toast.makeText(this, "请输入正确密码", Toast.LENGTH_SHORT).show();
  141.                 } else {
  142.                     loginSuccess();
  143.                 }
  144.             } else if (rb_verifycode.isChecked()) {
  145.                 if (mVerifyCode == null) {
  146.                     Toast.makeText(this, "请获取验证码", Toast.LENGTH_SHORT).show();
  147.                 }
  148.                 //验证码校验
  149.                 else if (!mVerifyCode.equals(et_password.getText().toString())) {
  150.                     Toast.makeText(this, "请输入正确验证码", Toast.LENGTH_SHORT).show();
  151.                 } else {
  152.                     loginSuccess();
  153.                 }
  154.             }
  155.         }
  156.     }
  157.     //模拟登录成功
  158.     private void loginSuccess() {
  159.         String desc = String.format("登录成功,手机号:%s", et_phone.getText().toString());
  160.         //弹出登录成功的对话框
  161.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  162.         builder.setTitle("登录成功");
  163.         builder.setMessage(desc);
  164.         builder.setPositiveButton("确定返回", (dialog, which) -> {
  165.             //TODO
  166.             finish();
  167.         });
  168.         builder.setNegativeButton("取消", null);
  169.         AlertDialog dialog = builder.create();
  170.         dialog.show();
  171.         if (ck_remember.isChecked()) {
  172.             SharedPreferences.Editor editor = preferences.edit();
  173.             editor.putString("phone", et_phone.getText().toString());
  174.             editor.putString("password", et_password.getText().toString());
  175.             editor.putBoolean("is_remember", ck_remember.isChecked());
  176.             editor.commit();
  177.         }
  178.     }
  179.     //自定义一个文本输入框的监听器,用于隐藏输入法
  180.     private class HideTextWatcher implements TextWatcher {
  181.         private EditText mView;
  182.         private int mMaxLength;
  183.         public HideTextWatcher(EditText v, int maxLength) {
  184.             this.mView = v;
  185.             this.mMaxLength = maxLength;
  186.         }
  187.         @Override
  188.         public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  189.         }
  190.         @Override
  191.         public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  192.         }
  193.         @Override
  194.         public void afterTextChanged(Editable editable) {
  195.             if (editable.toString().length() == mMaxLength) {
  196.                 //隐藏输入法
  197.                 ViewUtil.hideOneInputMethod(LoginMainActivity.this, mView);
  198.             }
  199.         }
  200.     }
  201. }
复制代码
3.2 LoginMainActivity.java

忘记暗码页面业务逻辑
  1. package com.example.login_view;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AlertDialog;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import java.util.Random;
  12. public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {
  13.     private String mPhone;
  14.     private String mVerifyCode = "";
  15.     private EditText et_password_first;
  16.     private EditText et_password_second;
  17.     private EditText et_verifycode;
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_login_forget);
  22.         //从上一个页面获取要修改的手机密码
  23.         mPhone = getIntent().getStringExtra("phone");
  24.         et_verifycode = findViewById(R.id.et_verifycode);
  25.         et_password_first = findViewById(R.id.et_password_first);
  26.         et_password_second = findViewById(R.id.et_password_second);
  27.         findViewById(R.id.btn_verifycode).setOnClickListener(this);
  28.         findViewById(R.id.btn_confirm).setOnClickListener(this);
  29.     }
  30.     @Override
  31.     public void onClick(View view) {
  32.         //生成验证码
  33.         if (view.getId() == R.id.btn_verifycode) {
  34.             //如果选中了验证码登录,生成六位随机验证码
  35.             mVerifyCode = String.format("%06d", new Random().nextInt(999999));
  36.             //弹出提醒对话框,提示用户记住六位验证码
  37.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  38.             builder.setTitle("请记住验证码");
  39.             builder.setMessage("手机号" + mPhone + "的验证码是:" + mVerifyCode + ",请输入验证码");
  40.             builder.setPositiveButton("确定", null);
  41.             AlertDialog dialog = builder.create();
  42.             dialog.show();
  43.         }
  44.         //点击确认按钮
  45.         if (view.getId() == R.id.btn_confirm) {
  46.             String password_first = et_password_first.getText().toString();
  47.             String password_second = et_password_second.getText().toString();
  48.             if (password_first.isEmpty() || password_second.isEmpty()) {//判断密码是否为空
  49.                 Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
  50.                 return;
  51.             }
  52.             if (et_verifycode.getText().toString().isEmpty()) {//判断验证码是否为空
  53.                 Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
  54.                 return;
  55.             }
  56.             //判断密码长度合法
  57.             if (password_first.length() < 6) {
  58.                 Toast.makeText(this, "请输入大于6位密码", Toast.LENGTH_SHORT).show();
  59.                 return;
  60.             }
  61.             //判断两次输入的密码是否一致
  62.             if (!password_first.equals(password_second)) {//密码一致,修改密码
  63.                 Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
  64.                 return;
  65.             }
  66.             Log.d("panbug", "onClick: " + mVerifyCode + " " + et_verifycode.getText().toString());
  67.             Log.d("panbug", "onClick: " + mVerifyCode.equals(et_verifycode.getText().toString()));
  68.             //判断验证码是否正确
  69.             if (!mVerifyCode.equals(et_verifycode.getText().toString())) {
  70.                 Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
  71.                 return;
  72.             } else {
  73.                 Intent intent = new Intent();
  74.                 intent.putExtra("new_password", password_first);
  75.                 setResult(Activity.RESULT_OK, intent);
  76.                 //验证码正确,修改密码
  77.                 Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
  78.                 finish();
  79.             }
  80.         }
  81.     }
  82. }
复制代码
3.3工具类ViewUtil

  1. package com.example.login_view.utils;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.view.View;
  5. import android.view.inputmethod.InputMethodManager;
  6. public class ViewUtil {
  7.     public static void hideOneInputMethod(Activity activity, View view) {
  8.         // 获取输入法管理器
  9.         InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  10.         // 隐藏输入法软键盘
  11.         imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  12.     }
  13. }
复制代码
4. 页面预览






免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表