Android 如何检查编辑文本是否为空

Android 如何检查编辑文本是否为空,android,android-edittext,android-alertdialog,Android,Android Edittext,Android Alertdialog,我正在开发一个应用程序,如果我点击一个按钮,它会检查所有的编辑文本是否为空或写了什么东西,并为该消息提供类似的警告框。我已经创建了一个方法,并在Onclick事件中调用它。但我无法获得所需的结果,因为语法错误不是dr,甚至logcat没有显示任何错误。请帮助让我出去……提前谢谢 ImageButton b2=(ImageButton)findViewById(R.id.imageButton2); b2.setOnClickListener(new OnClickListener

我正在开发一个应用程序,如果我点击一个按钮,它会检查所有的编辑文本是否为空或写了什么东西,并为该消息提供类似的警告框。我已经创建了一个方法,并在Onclick事件中调用它。但我无法获得所需的结果,因为语法错误不是dr,甚至logcat没有显示任何错误。请帮助让我出去……提前谢谢

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

        }


    }
    });

}
    }
//编辑

我认为你的理解是错误的,如果你的if语句是当每个字段都填写时,你的else语句是当其中一个字段为空时

----先前的答复----

你可以这样做

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

if(editText.getText().toString().length() == 0) {
     // editText is empty
}
您可以尝试以下方法:

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

    // Error , one or more editText are empty

}
else
{
    // all editText are not empty 
}

Mats和Houcine答案是有效的,但您甚至可以使用
isEmpty()
缩短答案:


如果不是期望的结果,你能描述一下当前的结果是什么吗。你是说,你没有得到任何错误,但是带有“Exception:Incomplet form”的警报对话框从来没有出现过,只有“you's done”对话框出现过?如果((f.length()>0)和((f1.length()>0)和((f2.length()>0)和((f4.length()>0)和((f5.length()>0))。。。。这里你是说所有的字段都不是空的,但是在你的对话框中你是说你错过了一个字段:这不是他在代码中已经在做的吗?是的,你是对的,我错过了
String f3=e3.getText().toString()
lines感谢大家对我所问问题的进一步回答实际上我的逻辑和语法都很好,但我只是错过了编写alertBoxObject.show()的一个简单步骤;这就是为什么没有警报框不显示。。。
if(editText.getText().toString().isEmpty()) {
     // editText is empty
} else {
     // editText is not empty
}