Determine whether two strings are equal and whether the edittext input is empty in Android and Java, and define the input number.

Source: Internet
Author: User

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 ("")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.