Str1.equals (str2) to judge, cannot use = or! = To determine the reason:
Because string is of the reference type rather than the basic data type, their comparison uses the address and value (equivalent to the pointer in C) for comparison, because they are different objects, there are different addresses, so str1! = Str2 is always true. Str1 = str2 is always false. Use str1.equals (str2) If you want to compare their values ). Use = or! = Is the comparison of objects, they are true condition: not only must be the same object, but also the value must be equal.
Therefore, you can determine whether the edittext input is null:
If ("". Equals (edittext. gettext (). tostring (). Trim ()))
Above lineCodeIt means:
"" Indicates null. Nothing is input.
Equals means "to compare with the things in the ARC behind equals. If the same, it is null. What is actually input in the ARC
Edittext. gettext () is to get the content. Tostring () is converted to a string. Trim () means to filter out the leading and trailing spaces after getting the content.
Set the edittext input limit in XML to numbers.ProgramThere is no need to judge, either nothing or a number.
Android: inputtype = "Number"
In the edittext attribute, you can also limit the maximum number of characters that can be entered, that is, length.
Android: maxlength = "3"
A comprehensive example:
If ("". Equals (ET. gettext (). tostring ()))
{
New alertdialog. Builder (this)
. Setmessage ("height cannot be blank ")
. Setpositivebutton ("OK", null)
. Show ();
Return;
}
You can also determine whether the input is null:
Medittext. gettext (). Length () = 0
Medittext. gettext (). tostring () = NULL
Medittext. gettext (). tostring (). Equals ("")