我做了一个简单的例子,如注册程序,我想要两个类型条件“not null”或等于。这里等于正在工作但是当我在上一次吐司之后运行这个程序时所有的吐司节目。
这是我的代码,
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (name.getText().toString().equals("")){
Toast tname = Toast.makeText(SignUp.this, "Please Enter Name", Toast.LENGTH_SHORT);
tname.show();}
if (email.getText().toString().equals("")){
Toast temail = Toast.makeText(SignUp.this, "Please Enter Email", Toast.LENGTH_SHORT);
temail.show();}
if (user.getText().toString().equals("")){
Toast tuser = Toast.makeText(SignUp.this, "Please Enter username", Toast.LENGTH_SHORT);
tuser.show();}
if (pass.getText().toString().equals("")){
Toast tpass = Toast.makeText(SignUp.this, "Please Enter password", Toast.LENGTH_SHORT);
tpass.show();}
if (pass2.getText().toString().equals("")){
Toast tpass2 = Toast.makeText(SignUp.this, "Please Re-Enter Password", Toast.LENGTH_SHORT);
tpass2.show();}
if(pass.getText().toString().equals(pass2.getText().toString()))
{
db.insertData(sname, semail, suser, spass);
name.setText("");
email.setText("");
user.setText("");
pass.setText("");
pass2.setText("");
Toast succ = Toast.makeText(SignUp.this, "Data Inserted Successfully", Toast.LENGTH_SHORT);
succ.show();
}else
{
Toast error = Toast.makeText(SignUp.this, "Password didn't match", Toast.LENGTH_SHORT);
error.show();
}
}
});
答案 0 :(得分:1)
试试这个: -
if (!(etEmail.getText().toString().matches(emailPattern))) {
Toast.makeText(getApplicationContext(), "Not valid email address", Toast.LENGTH_SHORT).show();
return;
} else if (etPassword.getText().toString().trim().length() <= 6) {
Toast.makeText(getApplicationContext(), "Password Lenght must be Greater then 6 character", Toast.LENGTH_SHORT).show();
return;
}
答案 1 :(得分:0)
要么:
if (name.getText().toString().equals("")){
Toast tname = Toast.makeText(SignUp.this, "Please Enter Name", Toast.LENGTH_SHORT);
tname.show();}
else if (email.getText().toString().equals("")){
Toast temail = Toast.makeText(SignUp.this, "Please Enter Email", Toast.LENGTH_SHORT);
temail.show();}
else
是一个关键工作
仅显示第一条错误消息。
或者如果您想一次显示所有错误消息,您可以从所有人创建一条消息,执行以下操作:
StringBuilder text = new StringBuilder();
if (name.getText().toString().equals("")){
text.append("Please Enter Name");}
if (email.getText().toString().equals("")){
text.append("Please Enter Email");}
// other conditions here
Toast.makeText(SignUp.this, text.toString(), Toast.LENGTH_SHORT).show();
希望这有帮助
答案 2 :(得分:0)
尝试这样做:
if (name.getText().toString().equals("")){
Toast tname = Toast.makeText(SignUp.this, "Please Enter Name", Toast.LENGTH_SHORT);
tname.show();}
if (email.getText().toString().equals("")){
Toast temail = Toast.makeText(SignUp.this, "Please Enter Email", Toast.LENGTH_SHORT);
temail.show();}
else if (user.getText().toString().equals("")){
Toast tuser = Toast.makeText(SignUp.this, "Please Enter username", Toast.LENGTH_SHORT);
tuser.show();}
else if (pass.getText().toString().equals("")){
Toast tpass = Toast.makeText(SignUp.this, "Please Enter password", Toast.LENGTH_SHORT);
tpass.show();}
else if (pass2.getText().toString().equals("")){
Toast tpass2 = Toast.makeText(SignUp.this, "Please Re-Enter Password", Toast.LENGTH_SHORT);
tpass2.show();}
else if(pass.getText().toString().equals(pass2.getText().toString()))
{
db.insertData(sname, semail, suser, spass);
name.setText("");
email.setText("");
user.setText("");
pass.setText("");
pass2.setText("");
Toast succ = Toast.makeText(SignUp.this, "Data Inserted Successfully", Toast.LENGTH_SHORT);
succ.show();
}else
{
Toast error = Toast.makeText(SignUp.this, "Password didn't match", Toast.LENGTH_SHORT);
error.show();
}
这基本上只是添加一个else if
,它将对语句进行分组,而不是为每个动作启动一个新的if语句。
希望这有帮助!