0

我正在注册一个 android 应用程序。最初,当密码和确认密码不一样时,会提示“密码和确认密码不匹配”,并且成功。但是,如果密码和确认密码与空白匹配,则应出现此语句,“不要将任何字段留空”,不幸的是它失败了。

请帮我检查是否有任何错误。

  btn2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                String username, password, cpassword, fullname, nric, address, phone, email;
                username = tf3.getText().toString();
                password = tf4.getText().toString();
                cpassword = tf5.getText().toString();
                fullname = tf6.getText().toString();
                nric = tf7.getText().toString();
                address = tf8.getText().toString();
                phone = tf9.getText().toString();
                email = tf10.getText().toString();

                if(password != cpassword)
                {
                    tv1.setText("Password & Confirm Password does not match.");
                }
                else if(username.equals("") || password.equals("") || cpassword.equals("") || fullname.equals("") || nric.equals("") || address.equals("") || phone.equals("") || email.equals(""))
                {
                    tv1.setText("Do not leave any field empty.");
                }
                else
                {
                    try
                    {
                        db.beginTransaction();
                        db.execSQL("insert into Members values('"+username+"','"+password+"','"+fullname+"','"+nric+"','"+address+"','"+phone+"','"+email+"');");
                        db.setTransactionSuccessful();
                        db.endTransaction();    
                    }
                    catch(Exception e)
                    {
                    }
                        tv1.setText("Register Complete.");
                }
            }
        });
4

2 回答 2

0

比较密码和确认密码字段如下。

if(!password.equals(cpassword)){

    tv1.setText("Password & Confirm Password does not match.");

}

希望它能帮助.. 总是使用equals()来比较两个字符串。

于 2013-04-24T06:57:33.857 回答
0

您以错误的方式比较它,只需比较您的密码和确认密码,如下所示,

if(!password.equals(cpassword))

在 Java 中,当您想要比较两个 String 变量时,您需要使用equals()String 类的方法。

        btn2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            String username, password, cpassword, fullname, nric, address, phone, email;
            username = tf3.getText().toString();
            password = tf4.getText().toString();
            cpassword = tf5.getText().toString();
            fullname = tf6.getText().toString();
            nric = tf7.getText().toString();
            address = tf8.getText().toString();
            phone = tf9.getText().toString();
            email = tf10.getText().toString();

            if(!password.equals(cpassword))  // Change is here
            {
                tv1.setText("Password & Confirm Password does not match.");
            }
            else if(username.equals("") || password.equals("") || cpassword.equals("") || fullname.equals("") || nric.equals("") || address.equals("") || phone.equals("") || email.equals(""))
            {
                tv1.setText("Do not leave any field empty.");
            }
            else
            {
                try
                {
                    db.beginTransaction();
                    db.execSQL("insert into Members values('"+username+"','"+password+"','"+fullname+"','"+nric+"','"+address+"','"+phone+"','"+email+"');");
                    db.setTransactionSuccessful();
                    db.endTransaction();    
                }
                catch(Exception e)
                {
                }
                    tv1.setText("Register Complete.");
            }
        }
    });
于 2013-04-24T04:10:24.977 回答