簡體   English   中英

Android AlertDialog如何在顯示之前禁用按鈕?

[英]Android AlertDialog how to disable buttons before showing?

如何在顯示警告對話框之前禁用按鈕,就像在Fatal對話框中完成一樣"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上啟用按鈕,則用戶可以在“確定”按鈕上單擊兩次。

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

我認為你應該默認禁用它。 並使用onShowListener()如下:

dlg.setOnShowListener(new OnShowListener() {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM