mercredi 29 avril 2015

If the user input is null or invalid, how can I make the code not do the next step

I have a data form and when the submit button is clicked, I'm checking if the user input in EditText is null or invalid. I want to make my code not go to confirmation window until the user input is not null and valid. How can I do this?

private void ifEmpty(EditText et, String message) {
    if (TextUtils.isEmpty(et.getText())) {
        Toast.makeText(MainActivity.this, message + "を入力してください。",     Toast.LENGTH_SHORT).show();
        return;
    }
}

private void ifEmptyAndNum(EditText et, String message) {
    if (TextUtils.isEmpty(et.getText())) {
        digitsOnly(et);
        Toast.makeText(MainActivity.this, "数字を入力してください。", Toast.LENGTH_SHORT)
                .show();
        Toast.makeText(MainActivity.this, message + "を入力してください。", Toast.LENGTH_SHORT)
                .show();
        return;
    }
}


@Override
public void onClick(View v) {
    ifEmpty(etName, "Name");
    ifEmptyAndNum(etAge, "Age");
    ifEmpty(etAddress, "Address");
    ifEmptyAndNum(etTel, "Phone Number");

    Intent intent = new Intent(this, ViewActivity.class);

    intent.putExtra("name", etName.getText().toString());
    intent.putExtra("age", etAge.getText().toString());
    intent.putExtra("address", etAddress.getText().toString());
    intent.putExtra("tel", etTel.getText().toString());
    startActivity(intent);

Aucun commentaire:

Enregistrer un commentaire