AlertDialog正面按钮变为空

时间:2012-10-20 15:01:04

标签: android alertdialog

我想在AlertDialog中禁用ok按钮。我尝试使用下面的代码。但获得NPE

alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(view);
alertDialogBuilder.setPositiveButton(android.R.string.ok, this);
alertDialogBuilder.setNegativeButton(android.R.string.cancel, this);
alertDialog = alertDialogBuilder.create();

Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positive.setEnabled(false);

按钮正数为空。

3 个答案:

答案 0 :(得分:0)

通过这种方式设置按钮,它只显示一个按钮。

alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(view);
alertDialog = alertDialogBuilder.create();

    alertDialog.setButton(android.R.string.cancel,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int arg1) {
                            dialog.dismiss();

                        }
                    });

答案 1 :(得分:0)

我找到了方法。重点是在显示警告对话框后,只有我们应该启用/禁用操作。 所以对于我在代码下面使用的禁用对话框。

alertDialog.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {

                Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                positive.setEnabled(false);

            }
        });

答案 2 :(得分:0)

只需使用神经按钮

  final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(super.mContext);
    dialogBuilder.setTitle(
            super.mContext.getResources().getString(R.string.FAVORITE_RENAME_DIALOG_TITLE));
    dialogBuilder.setMessage(
            super.mContext.getResources().getString(R.string.FAVORITE_RENAME_DIALOG_MESSAGE));

    final EditText input = new EditText(super.mContext);

    input.setInputType(
            InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE);
    input.setText(name);
    input.setSelectAllOnFocus(true);
    input.setHint(super.mContext.getResources().getString(R.string.FAVORITE_DIALOG_HINT));
    input.setFilters(
            new InputFilter[]{new DialogInputFilter(), new InputFilter.LengthFilter(20)});

    dialogBuilder.setView(input);

    dialogBuilder.setNeutralButton(
            super.mContext.getResources().getString(R.string.FAVORITE_RENAME_DIALOG_POSITIVE),
            new UpdateFavoriteListNameOnClickListener(name, favListId, input,
                    (Activity) super.mContext, this)
    );

    dialogBuilder.setNegativeButton(
            super.mContext.getResources().getString(R.string.FAVORITE_DIALOG_NEGATIVE),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {

                }
            }
    );

这里首先创建()然后获取alertDialog并获取Button并按照你想要的那样做...我用它进行MinLength验证

    dialogBuilder.create();
    final AlertDialog alertDialog =  dialogBuilder.show();
    Button button = ((AlertDialog)alertDialog).getButton(AlertDialog.BUTTON_NEUTRAL);

    input.addTextChangedListener(new TextMinLengthValidator(input, button) {
        @Override
        public void validate(EditText input, Button button) {
            if (input.getText().length() < ConfigurationProvider.getConfig().getMinFavoriteNameLength() && button != null) {
                button.setEnabled(false);
                input.setError(mContext.getResources().getString(R.string.FAVORITE_DIALOG_LENGTH_NEGATIVE));
            } else if(button != null) {
                button.setEnabled(true);
                input.setError(null);
            }
        }
    });