检查编辑文本是否为空时返回警报对话框

时间:2018-09-01 16:57:57

标签: android alertdialog android-alertdialog

我用三个EditTexts和一个发送值的按钮创建了AlertDialog。如果“编辑文本”之一为空,则应返回到“警报”对话框。

我使用此代码进行检查:

 if (TextUtils.isEmpty(edt_dialog_date.getText().toString())) {
       Toast.makeText(MainActivity.this, "Please enter Date", 
                 Toast.LENGTH_SHORT).show();
       return;
 }  

如果编辑文本为空,则关闭“警报”对话框并返回到“父活动”,我想要返回的是“警报对话框”本身。 该怎么做?

这是我的出处:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 builder.setTitle("Add New Bill");

 LayoutInflater inflater = this.getLayoutInflater();
 View custom_dialog = inflater.inflate(R.layout.dialog_custom, null);

EditText edtNo = (EditText) custom_dialog .findViewById(R.id.edt_no);
EditText edtName = (EditText) custom_dialog .findViewById(R.id.edt_name);
EditText edtAge = (EditText) custom_dialog .findViewById(R.id.edt_age);

builder.setView(custom_dialog);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
  if (TextUtils.isEmpty(edtNo.getText().toString())) {
       return;
  }
  if (TextUtils.isEmpty(edtName.getText().toString())) {
   return;
    }
  if(TextUtils.isEmpty(edtAge.getText().toString())) {
        return;
     }
    }
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
builder.show();     

2 个答案:

答案 0 :(得分:2)

由于默认情况下,AlertDialog按钮将dismiss()对话框,因此,为避免这种情况,您必须在显示对话框后立即覆盖按钮单击侦听器。您可以尝试以下代码段。

dialog.setOnShowListener(new DialogInterface.OnShowListener() {

   @Override
    public void onShow(DialogInterface dialogInterface) {

        Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            //Dismiss once everything is OK.
            dialog.dismiss();
        }
     });
    }
});
dialog.show();

答案 1 :(得分:1)

要停止关闭对话框,直到您检查所有“编辑”文本是否均已填入正确的信息,您必须将肯定的按钮代码保留为空,并取消否定的对话框。 之后,您必须定义

  

警告对话框= builder.create();

然后显示对话框并使用

  

dialog.getButton

设置为肯定按钮,然后将OnClickListener设置为类似于以下代码:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View custom_dialog = inflater.inflate(R.layout.dialog_custom, null);

EditText edtNo = (EditText) custom_dialog .findViewById(R.id.edt_no);
EditText edtName = (EditText) custom_dialog .findViewById(R.id.edt_name);
EditText edtAge = (EditText) custom_dialog .findViewById(R.id.edt_age);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
         // leave it empty 
   }

});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
     @Override
     public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
    }
});

builder.setView(custom_dialog);
AlertDialog dialog = builder.create();
dialog.show();
 dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
            // write check code
  if (TextUtils.isEmpty(edtNo.getText().toString())) {
       return;
  }
  if (TextUtils.isEmpty(edtName.getText().toString())) {
      return;
  }
  if(TextUtils.isEmpty(edtAge.getText().toString())) {
     return;
 }

// if every thing is Ok then dismiss dialog
            dialog.dismiss();
   }

}