安卓判断两次输入的密码是否相等且不为空

前言

在Android开发、学习的过程当中,设计一个登录、注册页面是最基础的能力。注册时需要判断用户输入的密码是否为空、两次输入的密码是否相等,并且为了方便用户使用,在注册成功之后,应当将用户名传递到登录页面的用户名输入框内。下面就简单地展示一下。

XML 文件

Login页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/fullscreenBackgroundColor"
    android:theme="@style/ThemeOverlay.KDAPP.FullscreenContainer"
    >

    <ImageView
        android:id="@+id/iv_share_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/loginpage"
        android:tileMode="disabled" />

    <Button
        android:id="@+id/login_login_button"
        android:layout_width="wrap_content"
        android:layout_height="65dp"
        android:layout_above="@+id/login_mines_title"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="15dp"
        android:fontFamily="@font/zh_blod"
        android:shadowColor="@color/white"
        android:text="登录"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/multi_function"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/login_signup_button"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="65dp"
            android:fontFamily="@font/zh_blod"
            android:shadowColor="@color/white"
            android:text="注册"
            android:textColor="@color/black"
            android:textColorHint="@color/white"
            android:textSize="20sp" />

        <ImageButton
            android:id="@+id/QQ"
            android:layout_width="0dp"
            android:layout_height="65dp"
            android:layout_weight="1"
            android:fontFamily="@font/blod"
            android:shadowColor="@color/white"
            android:src="@drawable/qq"
            android:scaleType="centerInside"
            android:textColor="@color/black"
            android:textColorHint="@color/white"
            android:textSize="15sp" />


        <ImageButton
            android:id="@+id/Wechat"
            android:layout_width="0dp"
            android:layout_height="65dp"
            android:layout_weight="1"
            android:fontFamily="@font/blod"
            android:shadowColor="@color/white"
            android:src="@drawable/wechat"
            android:scaleType="centerInside"
            android:textColor="@color/black"
            android:textColorHint="@color/white"
            android:textSize="15sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/login_main_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="5dp"
        android:fontFamily="@font/zh_blod"
        android:text="@string/loginpage_main_title"
        android:textColor="@color/white"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/login_mines_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/multi_function"
        android:layout_centerInParent="true"
        android:layout_marginStart="22dp"
        android:layout_marginBottom="15dp"
        android:fontFamily="@font/blod"
        android:text="@string/loginpage_page_mines_title"
        android:textColor="@color/white"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/editTextTextPassword"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/login_login_button"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="15dp"
        android:fontFamily="@font/blod"
        android:inputType="textPassword"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/login_password_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextTextPassword"
        android:layout_marginStart="25dp"
        android:layout_marginBottom="5dp"
        android:fontFamily="@font/zh_weibei"
        android:text="密码"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editTextTextUserName"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/login_password_textview"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/blod"
        android:inputType="textNoSuggestions"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/login_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextTextUserName"
        android:layout_marginStart="25dp"
        android:layout_marginBottom="5dp"
        android:fontFamily="@font/zh_weibei"
        android:text="用户名"
        android:textColor="@color/white"
        android:textSize="20sp" />

</RelativeLayout>

Sign Up 页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/fullscreenBackgroundColor"
    android:theme="@style/ThemeOverlay.KDAPP.FullscreenContainer"
    >

    <ImageView
        android:id="@+id/iv_share_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/loginpage"
        android:tileMode="disabled" />

    <Button
        android:id="@+id/signup_signup_button"
        android:layout_width="wrap_content"
        android:layout_height="65dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:fontFamily="@font/zh_blod"
        android:shadowColor="@color/white"
        android:shadowRadius="@android:integer/config_shortAnimTime"
        android:text="注册"
        android:textColor="@color/black"
        android:textColorHint="@color/white"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/login_main_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="5dp"
        android:fontFamily="@font/zh_blod"
        android:text="@string/signup_main_title"
        android:textColor="@color/white"
        android:textSize="50sp" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/signup_signup_button"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:inputType="text"
        android:fontFamily="@font/blod"
        android:textSize="15sp"
        android:textColor="@color/white"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextPhone"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/zh_weibei"
        android:text="邀请码"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editTextTextEmailAddress"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/textView"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/blod"
        android:textSize="15sp"
        android:inputType="textEmailAddress"
        android:textColor="@color/white"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextTextEmailAddress"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"

        android:textColor="@color/white"
        android:text="电子邮箱地址"
        android:fontFamily="@font/zh_weibei"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/editTextTextPassword4"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/textView2"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/blod"
        android:inputType="textPassword"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextTextPassword4"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:text="再次输入密码"
        android:textColor="@color/white"
        android:fontFamily="@font/zh_weibei"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/editTextTextPassword5"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/textView3"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/blod"
        android:inputType="textPassword"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextTextPassword5"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:text="密码"
        android:textColor="@color/white"
        android:fontFamily="@font/zh_weibei"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_above="@+id/textView4"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/blod"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:theme="@style/MyEditText" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextTextPersonName"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/zh_weibei"
        android:text="用户名"
        android:textColor="@color/white"
        android:textSize="20sp" />


</RelativeLayout>

Activity 中的动作判断代码

Login

package com.example.kdapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class login extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        //强制使用竖屏
        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();

        Intent getusername = getIntent();
        String getintentusername = null;
        getintentusername = getusername.getStringExtra("username");
        EditText username = findViewById(R.id.editTextTextUserName);

        if (getintentusername !=""){
            username.setText(getintentusername);
        }


        //注册按钮的逻辑操作
        Button button_signup = (Button) this.findViewById(R.id.login_signup_button);
        button_signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setClass(login.this,signup.class);
                //startActivity(intent1);
                startActivity(intent1, ActivityOptions.makeSceneTransitionAnimation(login.this).toBundle());
            }
        });
    }
}

Sign Up

package com.example.kdapp;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityOptions;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class signup extends AppCompatActivity {
    private EditText editTextTextPersonName;
    private EditText editTextTextPassword5;
    private EditText editTextTextPassword4;
    private Button button_signup;
    private String username = null;
    private String password1 = null;
    private String password2 = null;

    private View.OnClickListener PasswordCheck = new View.OnClickListener(){
        @Override
        public void onClick(View view){
            switch (view.getId()){
                case R.id.signup_signup_button:
                    //点击注册
                    username = editTextTextPersonName.getText().toString();
                    password1 = editTextTextPassword5.getText().toString();
                    password2 = editTextTextPassword4.getText().toString();
                    //验证密码是否一致
                    if(check()){
                        // 通过验证
                        Toast.makeText(signup.this,
                                "用户" + username + "注册成功!" + "\n" + "密码是" + password1,
                                Toast.LENGTH_SHORT).show();

                        //页面跳转,顺道传参
                        Intent intent1 = new Intent();
                        intent1.setClass(signup.this,login.class);
                        //startActivity(intent1);
                        intent1.putExtra("username",username);//传递注册成功的用户名
                        startActivity(intent1, ActivityOptions.makeSceneTransitionAnimation(signup.this).toBundle());

                    }else {
                        // 不能通过验证
                        Toast.makeText(signup.this, "用户" + username + "注册失败!",
                                Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        editTextTextPersonName = (EditText) findViewById(R.id.editTextTextPersonName);
        editTextTextPassword5 = (EditText)findViewById(R.id.editTextTextPassword5);
        editTextTextPassword4 = (EditText)findViewById(R.id.editTextTextPassword4);
        button_signup = (Button) this.findViewById(R.id.signup_signup_button);


        //强制使用竖屏
        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();

        //判断密码位置是否输入为空、输入是否相等

        button_signup.setOnClickListener(PasswordCheck);

    }

    private boolean check(){
        if ((password1.equals(password2)) && (username != "") && (username.length() != 0)
                && (password1 != "") && (password1.length() != 0))
            // 上述判断依次为密码一致性判断,用户名不为空判断,密码不为空判断
            return true;
        return false;
    }
}

实际效果

Screenshot_2022-04-13-12-08-51-544_com.example.kdapp

Screenshot_2022-04-13-12-08-55-399_com.example.kdapp

参考的博客

Android判断两次输入密码是否相等并且不为空

https://blog.csdn.net/Nobody_else_/article/details/80266124

Android开发--实现Android登录注册页面(上

https://blog.csdn.net/Waterme10n/article/details/122643169

Android学习之利用Intent传递数据

https://blog.csdn.net/qq_45637283/article/details/106454744

posted @ 2022-04-13 12:18  KD_Mercury  阅读(561)  评论(0编辑  收藏  举报
banniang