一次将错误设置为多个TextInputEditText

时间:2018-02-14 08:18:49

标签: java android

我使用多个TextInputEditTexts创建了一个注册活动。这里的事情是,当我按下登录按钮时,我希望包含错误的那些(例如null或无效)一次显示错误。我尝试使用if else if语句但它在它看到的第一个错误时停止。谁知道怎么做?

这是我的代码:

public class SignIn extends AppCompatActivity {

DatabaseHelper db = new DatabaseHelper(this);
private Spinner spnMonths;
private Spinner spnDays;
private TextInputEditText fName;
private TextInputEditText lName;
private TextInputEditText eMail;
private TextInputEditText userName;
private TextInputEditText password;
private TextInputEditText verifyPassword;
private CheckBox terms;
private Button btnSignIn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    fName = (TextInputEditText) findViewById(R.id.txtfirstName);
    lName = (TextInputEditText) findViewById(R.id.txtLastName);
    eMail = (TextInputEditText) findViewById(R.id.txtEmail);
    userName = (TextInputEditText) findViewById(R.id.txtUsername);
    password = (TextInputEditText) findViewById(R.id.txtPassword);
    verifyPassword = (TextInputEditText) findViewById(R.id.txtVerifyPassword);
    terms = (CheckBox) findViewById(R.id.chkTerms);
    btnSignIn = (Button) findViewById(R.id.btnSignIn);
    spnMonths = (Spinner) findViewById(R.id.spnMonth);
    spnDays = (Spinner) findViewById(R.id.spnDays);

    ArrayAdapter<String> adapterMonth = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.months));
    spnMonths.setAdapter(adapterMonth);

    spnMonths.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selectedMonth = spnMonths.getSelectedItem().toString();
            if (selectedMonth.equals("January") || selectedMonth.equals("March") || selectedMonth.equals("May") || selectedMonth.equals("June") || selectedMonth.equals("August") || selectedMonth.equals("October") || selectedMonth.equals("December")) {
                ArrayAdapter<String> adapterDays = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.daysJMMJAOD));
                spnDays.setAdapter(adapterDays);
            }

            else if (selectedMonth.equals("February")) {
                ArrayAdapter<String> adapterDays = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.dayFeb));
                spnDays.setAdapter(adapterDays);
            }

            else {
                ArrayAdapter<String> adapterDays = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.daysOther));
                spnDays.setAdapter(adapterDays);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            VerifySignin();
        }
    });
}

public void VerifySignin() {
    String fName_text = fName.getText().toString();
    String lName_text = lName.getText().toString();
    String username_text = userName.getText().toString();
    String email_text = eMail.getText().toString();
    String password_text = password.getText().toString();
    String verify_password = verifyPassword.getText().toString();
    Boolean chk;
    String month = spnMonths.getSelectedItem().toString();
    String day = spnDays.getSelectedItem().toString();
    String fullname = fName_text + " " + lName_text;
    String emptyText = "Field cannot be empty";
    if (terms.isChecked()) {
        chk = true;
    }
    else {
        chk = false;
    }
    if (TextUtils.isEmpty(fName_text.trim())) {
        fName.setError("Field cannot be empty");
    }

    else if (TextUtils.isEmpty(lName_text.trim())) {
        lName.setError("Field cannot be empty");
    }

    else if (TextUtils.isEmpty(username_text.trim())) {
        userName.setError("Field cannot be empty");
    }

    db.insertUserData(fullname, username_text, password_text, email_text);
    Toast.makeText(getApplicationContext(), "Successfully Signed In!" ,Toast.LENGTH_SHORT).show();
}

}

1 个答案:

答案 0 :(得分:0)

class BaseViewController: UIViewController{

override func viewDidLoad() {
    super.viewDidLoad()

    //Common initiation codes
}

// Write all the codes that can be shared between the controllers
}