In android, how does one determine that the data in edittext is null ?, Androidedittext

Source: Internet
Author: User

In android, how does one determine that the data in edittext is null ?, Androidedittext

Zookeeper wrote a simple memory program today, but found that its code always does not work when it judges that the data cannot be empty.

String titleStr = addtitle. getText (). toString (); String contentStr = addcontent. getText (). toString (); if (titleStr = null & contentStr = null) {Toast. makeText (this, "cannot add an empty data", Toast. LENGTH_SHORT ). show ();} else if (titleStr = null) {Toast. makeText (this, "add Title", Toast. LENGTH_SHORT ). show ();} else {DataUtils. addFile (titleStr, contentStr, this); Toast. makeText (this, "added successfully", Toast. LENGTH_SHORT ). show ();}

No ~~~

After improvement:

If (titleStr = "" & contentStr = ") {Toast. makeText (this, "cannot add an empty data", Toast. LENGTH_SHORT ). show ();} else if (titleStr = "") {Toast. makeText (this, "add Title", Toast. LENGTH_SHORT ). show ();} else {DataUtils. addFile (titleStr, contentStr, this); Toast. makeText (this, "added successfully", Toast. LENGTH_SHORT ). show ();}

Still unavailable

I have collected some information online. I have encountered more than this problem ~~~~ The solution is pasted as follows:

Usage of TextUtils. isEmpty () and equals () methods in android

When determining whether a user enters the user name or password on a UI, TextUtils is commonly used. the isEmpty () method is used to determine the value. However, you can use the equals () method to determine whether the value of EditText is null, I don't know which of the two methods is better? Why? Which of the following experts can help you with your answers? Thank you for choosing cainiao!

Please carefully read the official API: Returns true if the string is null or 0-length. It means to check whether the parameter is null or 0 length. Because you return a variable from EditText. If the variable itself is null, an error is returned if you drop its equals method. However, if you call TextUtils. isEmpty () to pass this variable as a parameter. If this parameter is null or "", the system returns true. Therefore, the official version is more rigorous. And. It is also very convenient. This is because you have to write an if statement to determine whether to use the statement. The returned value is a boolean value. Why can't someone else leave the road?

Is there a difference between TextUtils. isEmpty (str) and str. isEmpty? Which one is better? Please write the reason

When 'struct' is returned, the difference is that TextUtils is used when 'str' is null or an empty string. isEmpty (str) returns true; otherwise, false. When the value is null, str. isEmpty () will return a null pointer exception.

Given the efficiency, str. isEmpty () is better than TextUtils. isEmpty () is fast, because if you look at the official source code, you can find TextUtils. isEmpty () Judges two steps. First, Judge = null, and then judge. length = 0. While the former only judges. length = 0.

In terms of structure, TextUtils. isEmpty (str) is a static method, while str. isEmpty () is a method owned by a String object.

Thanks:
Common Methods of the TextUtils class: isEmpty and equals
Http://www.eoeandroid.com/thread-323960-1-1.html

_____________________________Sorry, forgive me. Thank you. I love you.

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.