Android AlertDialog如何在显示之前禁用按钮?

时间:2012-12-13 08:38:58

标签: java android alertdialog

如何在显示警告对话框之前禁用按钮,就像在致命对话框"Android error: The application has stopped unexpectedly please try again"上完成一样。

我使用这样的例子:

  @Override
  protected Dialog onCreateDialog(int id) {
    if (id == DIALOG) {
      Log.d(LOG_TAG, "Create");
      AlertDialog.Builder adb = new AlertDialog.Builder(this);
      adb.setTitle("Title");
      adb.setMessage("Message");
      adb.setPositiveButton("OK", null);
      dialog = adb.create();

      dialog.setOnShowListener(new OnShowListener() {
        public void onShow(DialogInterface dialog) {
          Log.d(LOG_TAG, "Show");
        }
      });

      dialog.setOnCancelListener(new OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
          Log.d(LOG_TAG, "Cancel");
        }
      });

      dialog.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
          Log.d(LOG_TAG, "Dismiss");
        }
      });
      return dialog;
    }
    return super.onCreateDialog(id);
  }

  public void onclick(View v) {
    showDialog(DIALOG);
  }

如果我在dialog.setOnShowListener上启用按钮,则用户可以在“确定”按钮上单击两次。

2 个答案:

答案 0 :(得分:2)

AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
//...All your code to set up the buttons initially

AlertDialog dialog = alertbox.create();
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) 
{
    button.setEnabled(false);
}

使用getButton启用和禁用

答案 1 :(得分:0)

我认为你应该默认禁用它。并使用onShowListener(),如下所示:

dlg.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                // TODO Auto-generated method stub
                //Enable buttons..
            }
        });