问题
I've added input validation to a set of edit texts in a dialog message using the setError
method in Android.
But when I test it by leaving some of the edit texts blank and clicking "ok"
on the dialog, the dialog closes without triggering a prompt to enter values.
Can anyone see what is wrong with this implementation?
I think it may be something to do with a call to close the dialog before input validation is triggered.
This is how I've set the input validation based on the 4th answer in this question, Check if EditText is empty.:
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
return;
}
}
dialog.cancel();
}
回答1:
That's because you are calling dialog.cancel()
irrespective of error(s). Call this method only if there is no error. Keep a track of error using a boolean. If error exist make it false otherwise keep it true and call the dialog.cancel()
method only if the boolean is true.
来源:https://stackoverflow.com/questions/27788043/how-to-validate-edit-text-input-in-android