【Android学习】简单的登录页面和业务逻辑实现

发布于:2024-05-09 ⋅ 阅读:(90) ⋅ 点赞:(0)

实现功能

1 登录页:密码登录和验证码登录
2 忘记密码页:修改密码
3 页面基础逻辑 java代码

基础页面

在这里插入图片描述
在这里插入图片描述

XML

login_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >
<!--    单选框-->
    <RadioGroup
        android:id="@+id/rg_login"
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rg_password"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="@string/login_by_password"
            android:textSize="@dimen/common_font_size"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/rg_vertifycode"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="@string/login_by_vertifycode"
            android:textSize="@dimen/common_font_size"/>
    </RadioGroup>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/phone_number"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size"/>
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:hint="@string/inout_phone_number"
            android:background="@drawable/editor_selector"
            android:textColor="@color/black"
            android:maxLength="11"
            android:inputType="number"
            android:textColorHint="@color/grey"
            android:textSize="@dimen/common_font_size"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/login_password"
            android:textColor="@color/black"
            android:textSize="@dimen/common_font_size"/>
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:inputType="numberPassword"
                android:hint="@string/input_password"
                android:background="@drawable/editor_selector"
                android:textColor="@color/black"
                android:maxLength="6"
                android:textColorHint="@color/grey"
                android:textSize="@dimen/common_font_size"/>
            <Button
                android:id="@+id/btn_forget"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="@string/forget_password"
                android:layout_alignParentEnd="true"
                android:textColorHint="@color/black"
                android:textSize="@dimen/common_font_size"
                />
        </RelativeLayout>
    </LinearLayout>
    <CheckBox
        android:id="@+id/ck_remember"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/remember_password"
        android:textColorHint="@color/black"
        android:textSize="@dimen/common_font_size"
        />
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textColorHint="@color/black"
        android:textSize="@dimen/btn_font_size"
        />
</LinearLayout>
login_forget_password.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:orientation="horizontal">
    <TextView

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/input_new_password"
        android:textColor="@color/black"
        android:textSize="@dimen/common_font_size"/>
    <EditText
        android:id="@+id/et_password_first"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:inputType="numberPassword"
        android:hint="@string/input_new_password_hint"
        android:background="@drawable/editor_selector"
        android:textColor="@color/black"
        android:maxLength="11"
        android:textColorHint="@color/grey"
        android:textSize="@dimen/common_font_size"/>
  </LinearLayout>
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:orientation="horizontal">
    <TextView

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/confirm_new_password"
        android:textColor="@color/black"
        android:textSize="@dimen/common_font_size"/>
    <EditText
        android:id="@+id/et_password_second"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:hint="@string/confirm_new_password_again"
        android:background="@drawable/editor_selector"
        android:textColor="@color/black"
        android:maxLength="11"
        android:inputType="number"
        android:textColorHint="@color/grey"
        android:textSize="@dimen/common_font_size"/>
  </LinearLayout>
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:orientation="horizontal">
    <TextView

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/verifycode"
        android:textColor="@color/black"
        android:textSize="@dimen/common_font_size"/>
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
      <EditText
          android:id="@+id/et_verifycode"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:layout_marginTop="5dp"
          android:layout_marginBottom="5dp"
          android:inputType="numberPassword"
          android:hint="@string/input_verifycode"
          android:background="@drawable/editor_selector"
          android:textColor="@color/black"
          android:maxLength="6"
          android:textColorHint="@color/grey"
          android:textSize="@dimen/common_font_size"/>
      <Button
          android:id="@+id/btn_verificode"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:text="@string/get_verifycode"
          android:layout_alignParentEnd="true"
          android:textColorHint="@color/black"
          android:textSize="@dimen/common_font_size"
          />
    </RelativeLayout>
  </LinearLayout>
  <Button
      android:id="@+id/btn_confirm"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/done"
      android:textColorHint="@color/black"
      android:textSize="@dimen/btn_font_size"
      />
</LinearLayout>

Java

loginMain.java
package com.example.learn;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BlendMode;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.learn.utils.ViewUtil;

import java.util.Random;

public class LoginMainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private EditText et_phone;
    private TextView tv_password;
    private EditText et_password;
    private Button btn_forget;
    private CheckBox ck_remember;
    private RadioButton rg_password;
    private RadioButton rg_vertifycode;
    private ActivityResultLauncher<Intent> register;
    private Button btn_login;
    //定义固定密码 调试使用
    private String mPassword = "111111";
    private String verifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main);
        RadioGroup rg_login = findViewById(R.id.rg_login);
        et_phone = findViewById(R.id.et_phone);
        tv_password = findViewById(R.id.tv_password);
        et_password = findViewById(R.id.et_password);
        btn_forget = findViewById(R.id.btn_forget);
        ck_remember = findViewById(R.id.ck_remember);
        rg_password = findViewById(R.id.rg_password);
        rg_vertifycode = findViewById(R.id.rg_vertifycode);
        btn_login = findViewById(R.id.btn_login);
        //设置监听器
        rg_login.setOnCheckedChangeListener(this);
        //为电话号码、密码增加文本长度变换监听器
        et_phone.addTextChangedListener(new HideKeyboardWatch(et_phone, 11));
        et_password.addTextChangedListener(new HideKeyboardWatch(et_password, 6));
        //为忘记密码/获取验证码增加点击事件
        btn_forget.setOnClickListener(this);
       //login按钮
        btn_login.setOnClickListener(this);
        //全局registerActivityForResult
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    //接收返回值
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        Intent intent = result.getData();
                        if(intent!=null && result.getResultCode()==Activity.RESULT_OK){
                            mPassword = intent.getStringExtra("new_password");
                        }
                    }
                });

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rg_password:
                //密码登录
                //验证码登录
                tv_password.setText(getString(R.string.login_password));
                et_password.setHint(getString(R.string.input_password));
                btn_forget.setText(getString(R.string.forget_password));
                ck_remember.setVisibility(View.VISIBLE);//显示
                break;
            case R.id.rg_vertifycode:
                //验证码登录
                tv_password.setText(getString(R.string.verifycode));
                et_password.setHint(getString(R.string.input_verifycode));
                btn_forget.setText(getString(R.string.get_verifycode));
                ck_remember.setVisibility(View.GONE);//隐藏
                break;


        }
    }

    @Override
    public void onClick(View view) {
        String phone = et_phone.getText().toString();
        if (phone.length() < 11) {
            Toast.makeText(this, "手机号码不足11位", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (view.getId()) {
            case R.id.btn_forget:
                //说明选择是密码登录
                // 此时按钮为忘记密码
                if (rg_password.isChecked()) {
                    //此时可将手机号传递给下一个忘记密码页面
                    Intent intent = new Intent(this, LoginForgetActivity.class);
                    intent.putExtra("phone", phone);
                    register.launch(intent);
                }else if(rg_vertifycode.isChecked()){
                    //生成6位随机验证码
                    verifyCode = String.format("%6d",new Random().nextInt(999999));
                    //弹出对话框 便于用户记住验证码
                    AlertDialog.Builder adb = new AlertDialog.Builder(this);
                    adb.setTitle("请记住验证码");
                    adb.setMessage("手机号"+phone+",验证码为"+verifyCode+",请输入验证码!");
                    adb.setPositiveButton("好的",null);
                    AlertDialog ad =adb.create();
                    ad.show();
                }
                break;
            case R.id.btn_login:
                //如果密码方式登录 判断密码是否正确
                if(rg_password.isChecked()){
                    if(!mPassword.equals(et_password.getText().toString())){
                        Toast.makeText(this,"请输入正确密码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    loginSuccess();
                }else if(rg_vertifycode.isChecked()){
                    if(!verifyCode.equals(et_password.getText().toString())){
                        Toast.makeText(this,"请输入正确验证码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    loginSuccess();
                }
                break;

        }
    }

    private void loginSuccess() {
        String desc=String.format("您的手机号码为%s,恭喜您登陆成功!",et_phone.getText().toString());
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        adb.setTitle("恭喜您登陆成功!");
        adb.setMessage("您的手机号为"+et_phone.getText().toString());
        adb.setNegativeButton("确定返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //结束当前活动页面
                finish();
            }
        });
        adb.setPositiveButton("我再看看",null);
        AlertDialog ad = adb.create();
        ad.show();

    }

    //编辑框监听器,当输入文本长度达到最大长度,隐藏键盘。
    private class HideKeyboardWatch implements TextWatcher {
        private EditText mView;
        private int maxLen;

        public HideKeyboardWatch(EditText et, int len) {
            this.mView = et;
            this.maxLen = len;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() == maxLen) {
                //隐藏软键盘
                ViewUtil.hideKeyboard(LoginMainActivity.this, mView);
            }
        }
    }
}
login_forget_password.java
package com.example.learn;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {

    private String mPhone;
    private String verifyCode;
    private EditText et_password_first;
    private EditText et_password_second;
    private EditText et_verifycode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget);
        mPhone = getIntent().getStringExtra("phone");
        findViewById(R.id.btn_verificode).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this); 
        et_password_first = findViewById(R.id.et_password_first);
        et_password_second = findViewById(R.id.et_password_second);
        et_verifycode = findViewById(R.id.et_verifycode);
    }


    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.btn_verificode:
                //生成6位随机验证码
                verifyCode = String.format("%6d",new Random().nextInt(999999));
                //弹出对话框 便于用户记住验证码
                AlertDialog.Builder adb = new AlertDialog.Builder(this);
                adb.setTitle("请记住验证码");
                adb.setMessage("手机号"+mPhone+",验证码为"+verifyCode+",请输入验证码!");
                adb.setPositiveButton("好的",null);
                AlertDialog ad =adb.create();
                ad.show();
                break;
            case R.id.btn_confirm:
                String pf = et_password_first.getText().toString();
                String ps = et_password_second.getText().toString();
                if(pf.length()<6){
                    Toast.makeText(this,"请输入正确密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!pf.equals(ps)){
                    Toast.makeText(this,"两次密码不同",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!verifyCode.equals(et_verifycode.getText().toString())){
                    Toast.makeText(this,"验证码不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"密码修改成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("new_password",pf);
                setResult(Activity.RESULT_OK,intent);
                finish();//结束当前页面


                break;
        }
    }
}