隐藏AlertDialog的“确定”和“取消”按钮

时间:2018-11-26 09:39:35

标签: android dialog

我想从AlertDialog隐藏按钮。我找到了this解决方案,但它只是禁用了按钮。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    ...
    AlertDialog dialog = builder.create();
    Button button = dialog.getButton(Dialog.BUTTON_POSITIVE);
    button.setEnabled(false);

    return dialog;
}

1 个答案:

答案 0 :(得分:0)

您必须将按钮的可见性设置为GONE,而不是使用setEnabled()。另外,您必须在对话框的onStart()中执行以下操作:

@Override
public void onStart() {
    super.onStart();

    AlertDialog d = (AlertDialog) getDialog();
    if (d != null) {
        Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
        Button negativeButton = d.getButton(Dialog.BUTTON_NEGATIVE);
        positiveButton.setVisibility(View.GONE);
        negativeButton.setVisibility(View.GONE);
    }
}