Возврат к диалоговому окну предупреждения при проверке, если текст редактирования пуст - PullRequest
0 голосов
/ 01 сентября 2018

Я создал AlertDialog с тремя EditTexts и кнопкой для отправки значений. если один из текстов редактирования пуст, он должен вернуться в диалог оповещения.

Я использую этот код для проверки:

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

Если текст редактирования пуст, он закрывает диалоговое окно Alert и возвращает к Родительскому действию. Я хочу вернуться к самому AlertDialog. как это сделать?

и это мой источник:

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 голосов
/ 03 сентября 2018

Чтобы прекратить закрывать диалоговое окно, пока вы не проверите, заполнен ли весь текст «Правка» правильной информацией, необходимо оставить пустой положительный код кнопки и закрыть диалоговое окно в отрицательной кнопке. После этого вы должны определить

Alert dialog = 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();
   }

}

0 голосов
/ 01 сентября 2018

Так как, по умолчанию, кнопки 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();
...