Validation on TextEdit boxes on android
I wounder if there is some different way o开发者_如何学编程f validating EditText on android controlls.. Or should I use the regular java way of doing this?
What kind of validation are we taking about?
Numbers:
android:numeric="decimal"
Length:
android:maxLength="10"
You can also provide "hints" of what the valid data should be:
android:hint="@string/numberHint"
Also see this answer
Depending on what your requirements are, you can look at the android:inputType attribute. This can be quite a bit easier in some cases. For example, specifying android:inputType=number
will simply disallow non-numeric symbols from being entered into the EditText box.
If you really want some fancy validation use the java.util.regex class
link text
(Matcher and Pattern)
Validation is somewhat tedious. Maybe I can help you in a more generic manner. I have written a basic framework to validate fields in Android.
It is totally free, and you can do whatever you like with it.
if(TextUtils.isEmpty(password)){
nPassword.setError("Password is required");
return;
}
if(password.length() < 6){
nPassword.setError("Password must be greater than 6 characters");
return;
}
''''''''''
"These are two sample validation, one checks if EditText is empty or not. Second one checks if length of the string in edit text is greater than 6 or not."
精彩评论