AlertDialog - 当用户点击“确定”时如何运行检查

时间:2011-01-24 18:21:11

标签: android alertdialog

对于自定义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 个答案:

答案 0 :(得分: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();
  }});

答案 1 :(得分:1)

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

AlertDialog.BuildersetView(View v)方法。因此,可以使用按钮添加自定义LinearLayout(在Dialog构建之前从资源中膨胀)。然后只需在按钮上设置常规android.view.View.OnClickListener(s)即可。在这种情况下,请不要将{内置/本机“用于AlertDialog.Builder按钮。

答案 2 :(得分: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;
    }
}

答案 3 :(得分:0)

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

如果您使用dialogBuilder's setNeutralButtonsetPositiveButtonsetNegativeButton,似乎没有简单的方法可以阻止对话被解雇。

相关问题