How to determine if the data in EditText is empty in Android

Source: Internet
Author: User
Tags empty

Today I wrote a simple memory note applet, but found that my own code did not work when I judged that the data added was not empty.

 String titlestr = AddTitle () ()
 String contentstr = Addcontent () ()
         if (titlestr = null && contentstr = = Nu ll) {
            Toast (this,, Toast_short) ()
        } else if (Titlestr = null) {
            Toast (this,, Toast_short) ()
        } else {
  datautils (Titlestr, Contentstr, this)
            Toast (this,, Toast_short) ()

        }

No ~ ~ ~

After improvement:

        if (titlestr = =  && Contentstr = =) {
            Toast (this,, Toast_short) ()
        } else if (titlestr = =) {
            T Oast (this,, Toast_short) ()
        } else {
            datautils (titlestr, Contentstr, this) Toast (this,
            , Toast_short) ()

        }

Still not.

On the Internet to collect some information, it turns out that I have not only encountered this problem AH ~ ~ ~ The solution is pasted as follows:

Use of the Textutils.isempty () and Equals () methods in Android

For a UI interface, we often use the Textutils.isempty () method to determine whether or not a user enters a username or password, but sometimes it can be used to determine whether the edittext is empty, but sometimes it is very tangled, I wonder which of the two methods is better? Why? Please who knows the knight detailed help answer, rookie ingredient thanks!

Carefully read the official Api:returns True if the string is null or 0-length. The meaning seems to be to see whether the parameter is empty or 0 length. Because you are returning a variable from EditText. If the variable itself is a null value, then you drop its Equals method to error. But if you call Textutils.isempty () pass the variable in as a parameter. As long as the argument is empty or "", it will return true. So, use the official to give more rigorous. And. is also very convenient. Because you are alone to judge that you are not going to write an if statement to judge. Return is a Boolean value, why do others paved the way not to go?

Is there a difference between textutils.isempty (str) and Str.isempty ()? Which is better? Please write the reason why

In the case of a return result, there is a difference between textutils.isempty (str) returning True when STR is empty (null) or empty string (""), or false. When empty (null) str.isempty () null pointer exception.

In terms of efficiency, str.isempty () faster than Textutils.isempty (), because if you look at the official source words can find Textutils.isempty () judged two steps, first judge = = NULL, and then judge. Length = = 0. And the former only judge. length = 0.

Structurally, Textutils.isempty (str) is a static method, and Str.isempty () is the method owned by the object of the String class.

Thanksgiving:

Common methods of Textutils class IsEmpty and equals

Http://www.eoeandroid.com/thread-323960-1-1.html

_____________________________ Sorry, please forgive me, thank you, I love you

Related Article

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.