0

在此处输入图像描述

if((noValidate.getText().toString().length()>0)  &&     (nameValidate.getText().toString().length()>0)  &&
            (conpassValidate.getText().toString() ==  passValidate.getText().toString()) &&  (email.matches(emailPattern))
            &&(passValidate.getText().toString().length()>0))
        {
        Toast.makeText(this, "Your Information are saved succesfully ",   Toast.LENGTH_SHORT).show();

        Intent getStarted = new Intent(getApplicationContext(),  FourthActivity.class);
        startActivity(getStarted);
        }
    else
    {
        Toast.makeText(this, "Field Requiered",Toast.LENGTH_SHORT ).show();
    }
}

在这里,当我单击登录按钮时,它将验证并汇总该字段。如果任何字段为空,它将显示该字段为必填项。如果我在所有字段中输入数据意味着它总是显示该字段是必需的。我不知道问题出在哪里。

4

1 回答 1

1

在 Java 中,不能使用等号运算符 (=) 比较字符串:

conpassValidate.getText().toString() ==  passValidate.getText().toString()

改用 equal 方法:

conpassValidate.getText().toString().equals(passValidate.getText().toString())

这将是完整的代码:

if((noValidate.getText().toString().length()>0)  &&     
   (nameValidate.getText().toString().length()>0)  &&
   (conpassValidate.getText().toString().equals(passValidate.getText().toString())) &&  
   (email.matches(emailPattern)) && 
   (passValidate.getText().toString().length()>0)) {

       Toast.makeText(this, "Your Information are saved succesfully ",   Toast.LENGTH_SHORT).show();
       Intent getStarted = new Intent(getApplicationContext(),  FourthActivity.class);
       startActivity(getStarted);
} else {
       Toast.makeText(this, "Field Requiered",Toast.LENGTH_SHORT ).show();
}

如果这仍然不起作用,请在 else 之后使用此代码打印您的验证

String tag = "Your Tag";
Log.d(tag, "1: " + noValidate.getText().toString().length()>0 ? "YES":"NO");
Log.d(tag, "2: " + nameValidate.getText().toString().length()>0 ? "YES":"NO");
Log.d(tag, "3: " + conpassValidate.getText().toString().equals(passValidate.getText().toString()) ? "YES":"NO");
Log.d(tag, "4: " + email.matches(emailPattern) ? "YES":"NO");
Log.d(tag, "5: " + passValidate.getText().toString().length()>0 ? "YES":"NO");
于 2013-07-27T11:04:53.003 回答