AlertDialog - how can I run checks when user hits

2020-03-24 05:16发布

For a custom AlertDialog, can I override the positive button to NOT close the dialog? instead I want to run some edit checks and keep the dialog open if my checks fail.

protected Dialog onCreateDialog(int id) {
  Dialog alertDialog = null;
  builder = new AlertDialog.Builder(this);
  switch(id) {
    case LOGIN_USERID_BLANK:
      builder.setMessage((String)getString(R.string.username_not_blank));
      builder.setPositiveButton((String)getString(R.string.ok), new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      // Can I do something here so that the dialog does not close?
}
});

break;

4条回答
不美不萌又怎样
2楼-- · 2020-03-24 05:28

Here's a workaround until Google changes the Dialog API:

LayoutInflater factory = LayoutInflater.from(this);
final View view = factory.inflate(R.layout.yoMamma, null);
final Dialog dlg = new AlertDialog.Builder(this)
    .setTitle(R.string.yoTitle)
    .setView(view)
    .setPositiveButton(R.string.dlgOK, new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        // This won't be called.
      }})
    .create();
dlg.show();
View button = ((AlertDialog)dlg).getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
    // This WILL be called.
    // Remove the following if you don't want the dialog to dismiss
    dlg.dismiss();
  }});
查看更多
家丑人穷心不美
3楼-- · 2020-03-24 05:41

Ok, here is just an idea of how it is possible to implement.

AlertDialog.Builder has setView(View v) method. So it is possible to add, say, a custom LinearLayout (inflated from resources right before Dialog building) with button(s). Then just set usual android.view.View.OnClickListener(s) on the button(s). In this case don't use those "built-in/native" for the AlertDialog.Builder buttons at all.

查看更多
爷的心禁止访问
4楼-- · 2020-03-24 05:43

Here's how I did it. Technically, it doesn't technically keep the dialog open, it closes it momentarily and re-opens it, but the net result is the same.

class MyAlertDialog implements OnDismissListener, OnCancelListener {
    final private EditText editText;
    final private AlertDialog alertDialog;
    final private EventManager eventManager;
    final private CategorySelector categorySelector;

    private Boolean canceled;

    MyAlertDialog(Context context) {
        editText = new EditText(context);
        alertDialog = buildAlertDialog(context);
        alertDialog.setOnDismissListener(this);
        alertDialog.setOnCancelListener(this);
        show();
    }

    private AlertDialog buildAlertDialog(Context context) {
        return new AlertDialog.Builder(context)
        .setTitle(context.getString(R.string.enter_name))
        .setMessage(context.getString(R.string.enter_name))
        .setView(editText)
        .setNeutralButton(context.getString(R.string.save_text), null)
        .setNegativeButton(context.getString(R.string.cancel_text), null).create();
    }

    public void show() {
        canceled = false;
        alertDialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if(!canceled) {
            final String name = editText.getText().toString();
            if(name.equals("")) {
                editText.setError("Please enter a non-empty name");
                show();
            } else {
                doWhateverYouWantHere(name);
            }
        }
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        canceled = true;
    }
}
查看更多
Root(大扎)
5楼-- · 2020-03-24 05:49

I faced the same issue wherein I was not able to stop dialog from getting dismissed even when the input I wanted to collected in dialog had validation issues. To resolve this issue, I added buttons in the dialog's custom view so that I have better control.

There does not seem to be clean way of stopping dialog from getting dismissed if you use dialogBuilder's setNeutralButton or setPositiveButton or setNegativeButton.

查看更多
登录 后发表回答