4

对于自定义 AlertDialog,我可以覆盖肯定按钮以不关闭对话框吗?相反,如果我的检查失败,我想运行一些编辑检查并保持对话框打开。

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?
}
});

休息;

4

4 回答 4

6

在 Google 更改 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();
  }});
于 2011-09-23T21:18:58.627 回答
1

好的,这只是一个关于如何实现的想法。

AlertDialog.BuildersetView(View v)方法。因此,可以添加一个LinearLayout带有按钮的自定义(在对话框构建之前从资源中膨胀)。android.view.View.OnClickListener(s)然后只需在按钮上设置通常。在这种情况下,根本不要使用那些“内置/原生”的AlertDialog.Builder按钮。

于 2011-01-24T18:59:00.760 回答
0

我遇到了同样的问题,即使我想在对话框中收集的输入有验证问题,我也无法阻止对话框被解散。为了解决这个问题,我在对话框的自定义视图中添加了按钮,以便更好地控制。

dialogBuilder's setNeutralButton如果您使用orsetPositiveButton或,似乎没有一种干净的方法可以阻止对话框被解雇setNegativeButton

于 2013-07-21T13:47:36.080 回答
0

这就是我的做法。从技术上讲,它不会在技术上保持对话框打开,它会暂时关闭它并重新打开它,但最终结果是相同的。

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;
    }
}
于 2011-09-08T17:51:45.260 回答