Android 默认情况下禁用AlertDialog的(正)按钮
默认情况下,如何禁用AndroidAndroid 默认情况下禁用AlertDialog的(正)按钮,android,android-edittext,android-alertdialog,Android,Android Edittext,Android Alertdialog,默认情况下,如何禁用AndroidAlertDialog的肯定按钮 在用户更改视图(在本例中为EditText)之前,希望禁用肯定按钮(在本例中为“保存”)似乎很正常 我知道我可以通过调用dialog.getButton(DialogInterface.button\u POSITIVE)来获取按钮,但是如果还没有调用show(),则此调用将返回null。您必须调用show()来访问警报对话框的按钮。因此,在调用show()之后在alertDialog上,您将获得负按钮并将其设置为禁用,如下所示
AlertDialog
的肯定按钮
在用户更改视图(在本例中为EditText
)之前,希望禁用肯定按钮(在本例中为“保存”)似乎很正常
我知道我可以通过调用dialog.getButton(DialogInterface.button\u POSITIVE)
来获取按钮,但是如果还没有调用show()
,则此调用将返回null。您必须调用show()来访问警报对话框的按钮。因此,在调用show()之后在alertDialog上,您将获得负按钮并将其设置为禁用,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert);
AlertDialog d = builder.show();
d.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
因此,默认情况下,它的负按钮将被禁用。您是否尝试在显示之前调用create?您最好使用
DialogFragment
类创建一个自定义对话框。它包含在DialogFragment类中。您是否可以向我们显示一些代码?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if(condition)
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
});
dialog.show();