我正在为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.");
}
}
});
答案 0 :(得分:0)
您正在以错误的方式对其进行比较,只需比较您的密码和&确认密码如下,
if(!password.equals(cpassword))
在Java中,如果要比较两个String变量,则需要使用String类的equals()
方法。
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.");
}
}
});
答案 1 :(得分:0)
比较密码和确认密码字段如下。
if(!password.equals(cpassword)){
tv1.setText("Password & Confirm Password does not match.");
}
希望它有所帮助..总是使用 equals()来比较两个字符串。