【问题标题】:How can I change positive/negative button background color in MaterialAlertDialogBuilder? [duplicate]如何在 MaterialAlertDialogBu​​ilder 中更改正/负按钮背景颜色? [复制]
【发布时间】:2019-10-08 12:56:08
【问题描述】:

我正在使用MaterialAlertDialogBuilder,因为我想要圆角以及日夜模式。请注意,我不能使用 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 行,因为在我的风格中,我在 style.xml 中有不同的父级,我用它来根据日/夜模式设置颜色。

问题:如何在 MaterialAlertDialogBu​​ilder 中更改正/负按钮背景颜色?

请注意,可绘制背景在 MaterialAlertDialogBuilder 中不起作用

代码:

public void showNotesDialog() {
        MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this, R.style.dialogBoxStyle);
        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.row_note_layout, null);
        alertDialogBuilder.setView(promptsView);

        etNote = promptsView.findViewById(R.id.et_note);
        String buttonName = getString(R.string.add);
        if (!getNotes.isEmpty()) {
            buttonName = getString(R.string.update);
            etNote.getText().clear();
            etNote.setText(getNotes);
        }

        alertDialogBuilder.setPositiveButton(buttonName, null);
        alertDialogBuilder.setNegativeButton(getString(R.string.cancel), null);
        alertDialogBuilder.setNeutralButton("Clear", null);

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.setCancelable(false);


        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button buttonPositive = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                Button buttonNegative = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
                ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.colorRed));
                Button buttonClear = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
                buttonClear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        etNote.getText().clear();
                        getNotes = "";
                        noteDesc.setText(getNotes);
                    }
                });

                buttonPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getNotes = etNote.getText().toString();
                        noteDesc.setText(getNotes);
                        dialog.dismiss();
                    }
                });

                buttonNegative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });


        alertDialog.show();
    }

样式 (v21)

 <style name="dialogBoxStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:background">?attr/day_colorWhite_night_colorVeryDarkBlackMostlyBlack</item>
        <item name="android:textColor">?attr/day_colorDarkGray_night_colorWhite</item>
        <item name="android:textColorAlertDialogListItem">?attr/day_colorDarkGray_night_colorWhite</item>
        <item name="android:textColorSecondary">?attr/day_colorDarkGray_night_colorWhite</item>
        <item name="android:textColorPrimary">?attr/day_colorDarkGray_night_colorWhite</item>
        <item name="colorAccent">?attr/day_colorDarkGray_night_colorWhite</item>
        <item name="android:typeface">normal</item>
        <item name="textColorAlertDialogListItem">?attr/day_colorDarkGray_night_colorWhite</item>
        <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
    </style>

    <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
        <item name="cornerFamily">rounded</item>
        <!--TODO: Please note that "cornerSize" will give effect on outside of Dialog Box border line, not where the row is inflated.-->
        <!--TODO: If you want to set Inside corner: app:cardCornerRadius="60dp"-->
        <item name="cornerSize">8dp</item>
    </style>

【问题讨论】:

  • 查看post
  • 也不要使用Theme.AppCompat.DayNight.Dialog.Alert 作为父级,而是使用ThemeOverlay.MaterialComponents.MaterialAlertDialog。由于 MaterialAlertDialog 使用的是 MaterialButtons,因此您可以更改按钮的样式,例如使用 OutlinedBox 样式,您可以在 this post 中找到
  • @amin 我没有任何关于styles.xml 中对话框样式的代码。对于styles.xml(V21),我已经发布了问题。

标签: android android-alertdialog material-components-android


【解决方案1】:

在您的对话框中尝试以下代码

alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.DKGRAY)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE)

更换背景请参考this post

【讨论】:

  • 我也求背景
  • 为此,您需要在您的 styles.xml 文件中进行更改
  • 是的,告诉我,什么类型的更改?你能建议我吗?确保您不要更改父级,因为它应该根据日/夜模式。
  • 检查更新的链接
猜你喜欢
  • 2015-06-04
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 2014-08-04
  • 2021-05-07
相关资源
最近更新 更多