如何停止 net toast show?

How can I stop net toast show?

我做了一个简单的例子,比如注册程序,我想要两种类型的条件 "not null" 或等于。这里等于正在工作,但是当我 运行 这个程序在上一个 toast 之后显示所有 toast。

这是我的代码,

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();
            }

        }
    });

试试这个:-

 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;
                }

要么:

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();

希望对您有所帮助

尝试这样做:

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 语句。

希望对您有所帮助!