简体   繁体   中英

Android how to check empty edittext

In my android code, I need to return value of z if there is empty value or space or special characters entered and my code is this ..

And anyone can you please explain the diff b/w

s.trim().length() > 0,

and

s!= 0, s!= ""

My code is as such:

char c = 'z';
        String z = Character.toString(c);
        String aChar1 = String.valueOf(z);
        if (firstName != null )
        {
            aChar1 = firstName.substring(0, 1);
        }

        String aChar2 = "";

        if (!middleName.isEmpty())
        {
        aChar2 = middleName.substring(0,1);
        }

        String aChar3 = "";

        if (!lastName.isEmpty())
        {
        aChar3 = lastName.substring(0,1);
        }

Use the method String.contains()

if(String.contains(" ") || String.contains("your characters"))

UPDATE

Try this easiest way:

    String str = "hai ";
    String rgx = "[$&+,:;=?@#|]";// include the unwanted characters within []

    Pattern sPattern = Pattern.compile("[$&+, :;=?@#|]");
    Matcher matcher = sPattern.matcher(str);
    if(matcher.find())
    {
        Toast.makeText(MainActivity.this, "Unwanted character found",Toast.LENGTH_LONG).show();
    }

You can use this code to check if the edittext is empty or not,

EditText editText;

if(editText.getText().length == 0)
{
    // Code for edittext empty
} else {
    // Code for edittext non-empty
}

First you cannot use s!=0 because s is an string , you cannot compare string with integer like that,second trim basically remove all of the spaces from the string and then s.trim.length() gives you the length of the string. So, if length is 0 that means the edittext is empty.

The best way is:

String text = Edittext.gettext.toString();

Then check if the String is empty or not

if(Text.length==0)
// do something

Hope this will resolve your problem

You can use this method

private boolean edittextValidation() {
    String firstName = edtFName.getText().toString();
    String middleName = edtMName.getText().toString();
    String lastName = edtLName.getText().toString();
    if (firstName.equals("")) {
        firstName.requestFocus();
        Toast.makeText(getApplicationContext(), " First Name is required.",
                Toast.LENGTH_SHORT).show();
        return false;
    } else if (middleName.equals("")) {
        middleName.requestFocus();
        Toast.makeText(getApplicationContext(), " Middle Name is required.",
                Toast.LENGTH_SHORT).show();

        return false;
    } else if (lastName.equals("")) {
        lastName.requestFocus();
        Toast.makeText(getApplicationContext(), " Last Name is required.",
                Toast.LENGTH_SHORT).show();

        return false;
      else{
        return true;
    }
}

The best way to do is to check if the resulting string is empty. Use isEmpty() for this (rather than comparing length to 0).

For blank space, use Contains(" ")

For special characters, use a RegEx.

EditText mEditText;
String sValue=mEdittText.getText.toString();

if(sValue.isEmpty() || sValue.Contains(" ")){
  //Edittext is Empty or contains blank space. Do whatever you want to do.
  }
EditText editText = (EditText) findViewById(R.id.editText);

str = editText.getText().toString().trim();

if (str.equals("")) {
    Toast.makeText(this, "Text is empty.", Toast.LENGTH_SHORT).show();

    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM