How to check edit text is empty [duplicate]

2019-08-21 06:36发布

问题:

This question already has an answer here:

  • Check if EditText is empty. [closed] 30 answers

I am developing a application in which if i click a button it woul d check that all the edit text are empty or something written and providing similar alert box for that message.I had made a method and calling it in Onclick event.But i am not able to get the desired result as syntax error is not dr even logcat not showing any error please help me out...Thanks in advance..

    ImageButton b2=(ImageButton)findViewById(R.id.imageButton2);

   b2.setOnClickListener(new OnClickListener()
   {

    @Override
    public void onClick(View arg0) {
        checkValue();

    }

    private void checkValue() {
        EditText e = (EditText) findViewById(R.id.editText1);
           EditText e1 = (EditText) findViewById(R.id.editText2);
           EditText e2 = (EditText) findViewById(R.id.editText3);
           EditText e3 = (EditText) findViewById(R.id.editText4);
           EditText e4 = (EditText) findViewById(R.id.editText5);
           EditText e5 = (EditText) findViewById(R.id.editText6);
        String f = e.getText().toString();
         String f1 = e1.getText().toString(); 
           String f2 = e2.getText().toString();
           String f3 = e3.getText().toString();
           String f4 = e4.getText().toString();
           String f5 = e5.getText().toString();

        if ((f.length()>0)&&(f1.length()>0)&&(f2.length()>0)&&(f3.length()>0)&&(f4.length()>0)&&(f5.length()>0)) { 

            AlertDialog alert= new AlertDialog.Builder(Test.this).create();
            alert.setTitle("Exception:Incomplete Form");
            alert.setMessage("Looks like you missed a field or made a mistake.Please ensure all fields of form are filled.Click OK to go to main page");

            alert.setButton("OK", new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int which) {
                      Intent i=new Intent(Test.this,ShpoonkleActivity.class);

                } }); 

           } 
        else
        {
            AlertDialog alert= new AlertDialog.Builder(Test.this).create();
            alert.setTitle("Your Form is successfully processed.Thanks");
            alert.setMessage("You are Done! Keep an eye out for a confirmation email. If you don't get one in the next 24h, open this form again, and make sure youremail is correct");

        }


    }
    });

}
    }

回答1:

you can try this :

if ((f.trim().equals('') || (f1.trim().equals('') ||........) { 

    // Error , one or more editText are empty

}
else
{
    // all editText are not empty 
}


回答2:

// Edit

I think you have it the wrong way, where your if statment is when every is filled in, your else when one of the fields is empty

---- previous answer ----

You can do it like this.

if(editText.getText().toString().length() > 0) {
     // editText is not empty
} 

if(editText.getText().toString().length() == 0) {
     // editText is empty
}


回答3:

Mats and Houcine answers are valid but you can even do shorter using isEmpty():

if(editText.getText().toString().isEmpty()) {
     // editText is empty
} else {
     // editText is not empty
}