用户提交数据时显示空字段的Toast

时间:2014-03-15 09:28:51

标签: android android-edittext toast

我有一个活动,其中有三个EditTexts:名字,中间名,姓氏和提交按钮。如果用户提交的字段为空白,则会为不同的字段显示不同的toast。这是一个单击按钮的示例!:

3 个答案:

答案 0 :(得分:2)

我假设您可以自己弄清楚如何使用按钮监听器。在该按钮侦听器中,您可以使用以下代码:

// create EditText references
EditText etFirstName = (EditText)findViewById(R.id.firstName);
EditText etMiddleName = (EditText)findViewById(R.id.middleName);
EditText etLastName = (EditText)findViewById(R.id.lastName);

// boolean variables that each represent whether or not the each field is blank
// if the EditText length is 0, it's true (blank); otherwise, it's false (filled)
boolean firstNameBlank = etFirstName.length() == 0;
boolean middleNameBlank = etMiddleName.length() == 0;
boolean lastNameBlank = etLastName.length() == 0;

if (firstNameBlank && middleNameBlank && lastNameBlank) {
    // toast all three blank  
}
else if (firstNameBlank && middleNameBlank) {
    // toast just first and middle are blank
}
else if (firstNameBlank && lastNameBlank) {
    // toast just first and last are blank
}
else if (middleNameBlank && lastNameBlank) {
    // toast just middle and last are blank
}
else if (firstNameBlank) {
    // toast just first is blank
}
else if (middleNameBlank) {
    // toast just middle is blank
}
else if (lastNameBlank) {
    // toast just last is blank
}
else {
    // none are blank
}

答案 1 :(得分:0)

试试这种方式

EditText e = (EditText)findViewById(R.id.EDITTEXT));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}

答案 2 :(得分:0)

实施提交onClick()的{​​{1}}事件,如下所示:

button