如何设置AlertDialog.Builder的Ne​​gativeButton unlickable

时间:2012-03-28 03:10:38

标签: android android-dialog

我的代码如下:

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Dialog");

dialog.setPositiveButton("Check", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface arg0, int arg1) {
    if(condition) {
      //set NegativeButton unclickable
    } else {
      //set NegativeButton clickable
    }
  } // end of onClick
});

dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface arg0, int arg1) {
    //do something
  }
});

如何设置NegativeButton点击和不可点击?

2 个答案:

答案 0 :(得分:1)

试试这个:

... ...

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

if(condition) {
  //set NegativeButton unclickable
  alertDialog.getButton(Dialog.BUTTON_NEGATIVE).setClickable(false);
} else {
  //set NegativeButton clickable
  alertDialog.getButton(Dialog.BUTTON_NEGATIVE).setClickable(true);
}

答案 1 :(得分:1)

使用以下代码检查onclick中的条件:

try {
 Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");   
 field.setAccessible(true);   
 field.set(dialog, false);
}
catch(Exception e) { 
 e.printStackTrace();  
}

这可以使它什么都不做,但它仍然可以点击。