AlertDialog - 我怎么能运行检查时,用户点击“确定”用户、我怎么能、AlertDialog

2023-09-06 15:36:49 作者:微笑╮安葬了眼泪的骄傲〆

有关自定义AlertDialog,可我覆盖了积极的按钮不关闭对话框?相反,我要运行一些编辑检查并保持对话,如果我检查失败开放。

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

突破;

推荐答案

下面是一个解决办法,直到谷歌改变了对话框API:

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