简体   繁体   English

如何使用弹出消息或警报对话框验证多个edittext字段?

[英]How to validate multiple edittext fields with a pop-up message or alert dialog box?

I'd like to know how to validate my 4 edittext fields if one or more of these fields are left empty after tapping the button to process the inputs. 如果要在点击按钮处理输入后将其中一个或多个字段留空,我想知道如何验证我的4个edittext字段。 I have searched many solutions like using toast but It think it's not appropriate for multiple edittext fields and using textwatchers. 我搜索了许多解决方案,例如使用Toast,但它认为它不适用于多个edittext字段和textwatcher。 I'd like the app to show a pop-up message or alert dialog box saying "Please fill up the necessary fields." 我希望该应用显示弹出消息或警告对话框,提示“请填写必填字段”。

Any help would be appreciated. 任何帮助,将不胜感激。

You can use below common function for checking the Null values of the edittext: 您可以使用下面的常用功能来检查edittext的Null值:

  public static boolean m_isError; public static void checkEntryForEmptyValue(EditText p_editText, String p_nullMsg) { if (p_editText != null || p_nullMsg != null) { // use trim() while checking for blank values if ((p_editText.getText().toString().trim().equalsIgnoreCase("")) || (p_editText.getText().toString().trim().length() <= 0)) { m_isError = true; p_editText.setError(p_nullMsg); p_editText.requestFocus(); } } } } 

Use the above function as below inside your button click listener: 在按钮点击监听器中使用以下功能,如下所示:

 CommonUtil.m_isError = false; CommonUtil.checkEntryForEmptyValue(edittext,getResources(). getString(R.string.MessageEmpty)); if (!CustomValidator.m_isError) { Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_SHORT).show(); } else { //Your dialog with the error messages. } 

u can use some tooltips for validation like qtip or poshytip 您可以使用一些工具提示进行验证,例如qtip或poshytip

http://vadikom.com/demos/poshytip/ http://vadikom.com/demos/poshytip/

http://craigsworks.com/projects/qtip/ http://craigsworks.com/projects/qtip/

Write a validation function to check all text fields and append the tooltip object with the corresponding fields which fails the validation. 编写验证功能以检查所有文本字段,并在工具提示对象后面附加验证失败的相应字段。

The Simplest soulution is that check that if the fields are empty then show dialog here is simple code snippet 最简单的解决方案是检查是否字段为空,然后在此处显示对话框是简单的代码片段

  private void checkEntries()
{

    if(!(email.getText().toString().equals("")))    
    {
        if(!(pass.getText().toString().equals("")))
        {
            if(UIHelper.getInstance().emailAddressValidation(email.getText().toString()))
            {
                if(pass.getText().length()>=5)
                {
                    sendLoginRequest(email.getText().toString(),pass.getText().toString(),Constants.PHONE_ID);
                }
                else
                {
                    dialogBoxInUIthread("String","Password length should be greater than 5 ",LoginController.this,true);
                }
            }
            else
            {

                dialogBoxInUIthread("String","Invalid Email Id",LoginController.this,true);

            }   
        }
        else
        {
            dialogBoxInUIthread("String","Please enter password",LoginController.this,true);
        }
    }
    else
    {
        dialogBoxInUIthread("String","Please enter email",LoginController.this,true);
    }

}



private void dialogBoxInUIthread(final String title,final String msg, Context context,final boolean completed) {
         /* runOnUiThread(new Runnable() {
           public void run() {*/
            AlertDialog.Builder alertbox = new AlertDialog.Builder(LoginController.this);
            alertbox.setTitle(title);
            alertbox.setMessage(msg);
            alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface arg0, int arg1) {
                   if(completed){
                   }else{ }
               }
              });alertbox.show();
          /* }
          });*/
    }

Use this validate function when you click on button and you can check the alert message after method is executed 单击按钮时使用此验证功能,执行方法后可以检查警报消息

boolean flag_1= true,flag_2=true,flag_3=true;
String alertmsg;

.

private boolean validate()
{
    EditText et1    = (EditText)findViewById(R.id.et1);
    EditText et2    = (EditText)findViewById(R.id.et2);
    EditText et3    = (EditText)findViewById(R.id.et3);


    if(et1.getText().toString().isEmpty())
    {
        alertmsg+=  "Please fill 1st\n";
        flag_1  =   false;
    }
    if(et2.getText().toString().isEmpty())
    {
        alertmsg+=  "Please fill 2nd\n";
        flag_2  =   false;
    }
    if(et3.getText().toString().isEmpty())
    {
        alertmsg+=  "Please fill 3rd";
        flag_3  =   false;  
    }

    return flag_1||flag_2||flag_3;
}

Try this : 尝试这个 :

EDIT: 编辑:

Call this onClick of your process-input button: 调用过程输入按钮的onClick

RelativeLayout rl = (RelativeLayout) findViewById(R.id.mRlayout1);
boolean success = formIsValid(rl);
if(success == false){
  // alert dialog box
}
else{
  // process ahead
}

Declare this function: 声明此功能:

EDIT: 编辑:

public boolean formIsValid(RelativeLayout layout) {
    for (int i = 0; i < layout.getChildCount(); i++) {
        View v = layout.getChildAt(i);
        Class<? extends View> c = v.getClass();
        if (c == EditText.class) {
            EditText et = (EditText) v;
            if(et.getText().toString().equals(""))
                return false;
            //Toast.makeText(getApplicationContext(), ""+et.getText().toString(), Toast.LENGTH_LONG).show();                
        } 
    }
    return true;
}

By this you can validate N number of input controls with single call . 通过这个,你可以验证具有单呼 输入控件的N多

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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