You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android注册页面确认密码验证异常问题求助

Android注册页密码确认验证异常解决

问题根源

你的代码存在两个关键错误,导致密码匹配验证逻辑完全失效:

  • 确认密码文本获取错误:你调用了 conPassword.toString().toString(),这会返回EditText控件的对象字符串(比如android.widget.EditText@xxxxxx),而非用户输入的文本,正确写法是conPassword.getText().toString()。
  • 条件判断逻辑颠倒:你用passwordTxt.equals(conPasswordTxt)作为“密码不匹配”的判断条件,这意味着密码相等时反而会弹出不匹配提示,正确逻辑应该是判断!passwordTxt.equals(conPasswordTxt)(密码不相等时触发提示)。

修正后的完整代码

package com.example.loginregisterfirebase;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class Register extends AppCompatActivity {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://loginregisterfirebase-9fe2f-default-rtdb.firebaseio.com/");
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        final EditText fullname = findViewById(R.id.fullname);
        final EditText email = findViewById(R.id.email);
        final EditText phone = findViewById(R.id.phone);
        final EditText password = findViewById(R.id.password);
        final EditText conPassword = findViewById(R.id.conPassword);

        final Button registerBtn = findViewById(R.id.registrationBtn);
        final TextView loginNowBtn = findViewById(R.id.loginNowBtn);

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String fullnameTxt = fullname.getText().toString();
                final String emailTxt = email.getText().toString();
                final String phoneTxt = phone.getText().toString();
                final String passwordTxt = password.getText().toString();
                // 修正:获取确认密码的正确方式
                final String conPasswordTxt = conPassword.getText().toString();

                if (fullnameTxt.isEmpty()||emailTxt.isEmpty()||phoneTxt.isEmpty()||passwordTxt.isEmpty()||conPasswordTxt.isEmpty()){
                    Toast.makeText(Register.this, "please fill all the fields", Toast.LENGTH_SHORT).show();
                }
                // 修正:判断条件取反,密码不相等时提示
                else if (!passwordTxt.equals(conPasswordTxt)){
                    Toast.makeText(Register.this, "Passwords do NOT match", Toast.LENGTH_SHORT).show();
                }
                else{
                    databaseReference.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            if (snapshot.hasChild(phoneTxt)){
                                Toast.makeText(Register.this, "Phone number is already registered", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                databaseReference.child("users").child(phoneTxt).child("fullname").setValue(fullnameTxt);
                                databaseReference.child("users").child(phoneTxt).child("email").setValue(emailTxt);
                                databaseReference.child("users").child(phoneTxt).child("password").setValue(passwordTxt);

                                Toast.makeText(Register.this, "User registered successfully", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });

                }

            }

        });
        loginNowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}

额外提示

  • 建议对密码输入框设置inputType="textPassword",确保输入内容隐藏,提升安全性。
  • 可以添加密码长度校验(比如至少6位),进一步优化注册逻辑。

内容的提问来源于stack exchange,提问作者Nascent Suman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 02:10:27