空的 editText 字段在所有字段上显示错误 [Android]

Empty editText field shows error on all fields [Android]

我有五个 editText 字段,我想验证是否有空的 editText。 我的代码运行正常,问题是当有一个空字段时,所有字段都显示错误。

这是我的代码:

            String one = firstname.getText().toString();
            String two = lastname.getText().toString();
            String three = email.getText().toString();
            String four = address.getText().toString();
            String five = mobile.getText().toString();

            if(!TextUtils.isEmpty(one) && !TextUtils.isEmpty(two) &&
                    !TextUtils.isEmpty(three) &&!TextUtils.isEmpty(four) &&
                    !TextUtils.isEmpty(five)) {
                Intent i = new Intent(main.this, next.class);
                i.putExtra("first", one);
                i.putExtra("last", two);
                i.putExtra("email", three);
                i.putExtra("mobile", four);
                i.putExtra("address", five);
                startActivity(i);
            } else {
                firstname.setError("First name cannot be empty");
                lastname.setError("last name cannot be empty");
                email.setError("email cannot be empty");
                address.setError("address cannot be empty");
                mobile.setError("mobile cannot be empty");

            }

你做 if 一次检查所有的东西,像这样分开做

 Intent i = new Intent(main.this, next.class);

        if (!TextUtils.isEmpty(one)) {
            i.putExtra("first", one);
        } else {
            firstname.setError("First name cannot be empty");
        }

        if (!TextUtils.isEmpty(two)) {
            i.putExtra("last", two);

        } else {
            lastname.setError("last name cannot be empty");
        }
        ......

试试,

Boolean valid = true;
if ( TextUtils.isEmpty(one) ) {
    firstname.setError("First name cannot be empty");
    valid = false;
} 

if ( TextUtils.isEmpty(two) ) {
    lastname.setError("last name cannot be empty");
    valid = false;
}

if ( TextUtils.isEmpty(three) ) {
    email.setError("email cannot be empty");
    valid = false;
}

if ( TextUtils.isEmpty(four) ) {
    address.setError("address cannot be empty");
    valid = false;
}

if ( TextUtils.isEmpty(five) ) {
    mobile.setError("mobile cannot be empty");
    valid = false;
}

if ( valid ) {
   Intent i = new Intent(main.this, next.class);
   i.putExtra("first", one);
   i.putExtra("last", two);
   i.putExtra("email", three);
   i.putExtra("mobile", four);
   i.putExtra("address", five);
   startActivity(i);
}

因为在 else 的情况下,您在每个字段上都设置了错误 -

            firstname.setError("First name cannot be empty");
            lastname.setError("last name cannot be empty");
            email.setError("email cannot be empty");
            address.setError("address cannot be empty");
            mobile.setError("mobile cannot be empty");

------------------------以上错误---------------- ----

具体尝试像下面这样分离 if 情况 -

if ( TextUtils.isEmpty(one) ) {
    firstname.setError("First name cannot be empty");
}
if ( TextUtils.isEmpty(two) {
    lastname.setError("last name cannot be empty");
}

......and so on.

希望对您有所帮助。

这是一个简单的示例,您可以在此特定情况下重用代码。


...
//Usage
 if(validate(firstname,"First name") && validate(lastname, "last name") &&
                    validate(email,"email") &&validate(address,"address") &&
                    validate(mobile,"Mobile")) {
                Intent i = new Intent(main.this, next.class);
                i.putExtra("first", one);
                i.putExtra("last", two);
                i.putExtra("email", three);
                i.putExtra("mobile", four);
                i.putExtra("address", five);
                startActivity(i);
            }
        // No need of else statement

....
// Create this method somewhere in your activity
boolean validate(EditText editText, String label){

     if(TextUtils.isEmpty(editText.getText().toString())){
           editText.setError(label+" cannot be empty");
           return false;
     }
   return true;
}

Here is the basic logic where you are doing wrong, your code is saying if anyone from these four ( one, two , three , four ) is empty then your if statement will o execute, else statement will execute, so in else all your edit text will show error. So now you need to check like this,

    if (!TextUtils.isEmpty(one)) {
        i.putExtra("first", one);
    } else {
        firstname.setError("First name cannot be empty");
        return;
    }

    if (!TextUtils.isEmpty(two)) {
        i.putExtra("last", two);

    } else {
        lastname.setError("last name cannot be empty");
        return;
    }
    if (!TextUtils.isEmpty(three)) {
        i.putExtra("last", three);

    } else {
        lastname.setError("last name cannot be empty");
      return;
    }
     if (!TextUtils.isEmpty(four)) {
        i.putExtra("last", four);

    } else {
        lastname.setError("last name cannot be empty");
       return;
    }
   if (!TextUtils.isEmpty(five)) {
        i.putExtra("last", five);

    } else {
        lastname.setError("last name cannot be empty");
        return;
    }