0

如何在显示警报对话框之前禁用按钮,就像在 Fatal dialog 上所做的那样"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 上启用按钮,则用户可以在 OK 按钮上单击两次。

4

2 回答 2

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 启用和禁用

于 2012-12-13T09:03:32.410 回答
0

我认为您应该默认禁用它。并使用 onShowListener() 如下:

dlg.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                // TODO Auto-generated method stub
                //Enable buttons..
            }
        });
于 2012-12-13T08:49:14.860 回答