Пустое поле editText показывает ошибку во всех полях [Android] - PullRequest
0 голосов
/ 03 марта 2020

У меня есть пять полей 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");

            }

Ответы [ 5 ]

1 голос
/ 03 марта 2020

Попробуйте,

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);
}
0 голосов
/ 03 марта 2020

Вот основные c logi c, где вы делаете неправильно, ваш код говорит, что если кто-то из этих четырех (один, два, три, четыре) пуст, то ваш оператор if будет исполняться Оператор else будет выполнен, так что в противном случае весь ваш текст редактирования будет отображать ошибку. Так что теперь вам нужно проверить, как это,

    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;
    }
0 голосов
/ 03 марта 2020

Вот один простой пример, где вы можете повторно использовать код в данном конкретном случае.


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

0 голосов
/ 03 марта 2020

Потому что в 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");

------------------------ -выше неправильно ---------------------

Для уточнения c попробуйте отделить регистр 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.

Надеюсь, это поможет вам.

0 голосов
/ 03 марта 2020

вы делаете, если проверяете все сразу, делайте это отдельно, как это

 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");
        }
        ......
...