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

标签 java android android-alertdialog

如何在显示警报对话框之前禁用按钮,就像在致命对话框中所做的那样“Android错误:应用程序已意外停止,请重试”

我用这样的例子:

  @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上启用按钮,那么用户可以在“确定”按钮上单击两次。

最佳答案

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

关于java - Android AlertDialog 如何在显示之前禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855951/

相关文章:

android - ScrollView 和 Gallery 干扰

java - Android:更改自定义 AlertDialog 背景

android - 当用户点击后退按钮退出应用程序时显示警告

java - JUnit : org. junit.experimental.Theories

java - 里面有 listview 的 Viewpager

android - 使用 RxJava 和播放服务视觉时的致命信号 11 (SIGSEGV)

android - 如何在 Android 上显示警报对话框?

java - 数据表参数为 Null ,导致 NumberFormatException :Null. 有解决方案吗?

java - file.getPath() 的相对路径

android - OnItemClickListener 无法解析为类型(在 Fragment 内)