如何在安卓5中更改默认对话框按钮文本颜色

214

我在我的应用程序中有许多警报对话框。它是默认布局,但我正在向对话框添加积极和消极按钮。因此,按钮获得Android 5的默认文本颜色(绿色)。我尝试过更改它,但没有成功。有什么办法可以更改那个文本颜色吗?

我的自定义对话框:

public class MyCustomDialog extends AlertDialog.Builder {

    public MyCustomDialog(Context context,String title,String message) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View viewDialog = inflater.inflate(R.layout.dialog_simple, null, false);

        TextView titleTextView = (TextView)viewDialog.findViewById(R.id.title);
        titleTextView.setText(title);
        TextView messageTextView = (TextView)viewDialog.findViewById(R.id.message);
        messageTextView.setText(message);

        this.setCancelable(false);

        this.setView(viewDialog);

    } }

创建对话框:

MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ...
                        }
}).show();

那个 negativeButton 是一个默认的对话框按钮,它会从 Android 5 Lollipop 中获取默认的绿色。

非常感谢

带有绿色按钮的自定义对话框


几乎是重复的问题/答案https://dev59.com/6F8d5IYBdhLWcg3wxUjC#29810469,我觉得现在更适用。 - Jon Adams
18个回答

6
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorPrimary">#00397F</item>
    <item name="android:textColorPrimary">#22397F</item>
    <item name="android:colorAccent">#00397F</item>
    <item name="colorPrimaryDark">#22397F</item>
</style>

使用appcompat也可以更改按钮和其他文本的颜色:


Theme.AppCompat.Light.Dialog.Alert 可以很好地将按钮颜色更改为白色。 - Leo K

5

使用styles.xml(值)

非常简单的解决方案,将colorPrimary更改为您选择的颜色,即可更改警报框按钮文本的颜色。

<style name="MyAlertDialogStyle" parent="android:Theme.Material.Dialog.Alert">


        <!-- Used for the buttons -->
        <item name="colorAccent">@android:color/white</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">@color/black</item>

        <!-- Used for the background -->
        <item name="android:background">#ffffff</item>
        <item name="android:colorPrimary">@color/white</item>
        <item name="android:colorAccent">@color/white</item>
        <item name="android:windowEnterAnimation">@anim/bottom_left_enter</item>
    </style>

备选方案(使用Java)

 @SuppressLint("ResourceAsColor")
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {

                AlertDialog dialog = new AlertDialog.Builder(view.getContext(), R.style.MyAlertDialogStyle)

                        .setTitle("Royal Frolics")
                        .setIcon(R.mipmap.ic_launcher)
                        .setMessage(message)
                        .setPositiveButton("OK", (dialog1, which) -> {
                            //do nothing
                            result.confirm();
                        }).create();

                Objects.requireNonNull(dialog.getWindow()).getAttributes().windowAnimations = R.style.MyAlertDialogStyle;
                dialog.show();
                
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(R.color.white);
                return true;

            }

4

顺便提一下:

按钮的颜色(以及整个样式)也取决于当前主题,当您使用不同的主题时,它可能会有所不同。

android.app.AlertDialog.Builder builder = new AlertDialog.Builder()

或者

android.support.v7.app.AlertDialog.Builder builder = new AlertDialog.Builder()

最好使用第二个。


2
这是一个自定义主题,可以更改AlertDialog按钮的文本颜色。 它在我的设备上(三星A70 - 安卓11)运行良好。
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!--Support for other devices, I think so-->
        <item name="android:textColor">@color/yourcolor</item>
        <item name="colorButtonNormal">@color/yourcolor</item>
        <item name="colorAccent">@color/yourcolor</item>
    <!--only this code works on my device-->
        <item name="buttonBarButtonStyle">@style/MyButtonStyle</item>
    </style>

    <!--only this code works on my device-->
    <style name="MyButtonStyle" parent="Widget.AppCompat.Button.Borderless">
        <item name="android:textColor">@color/yourcolor</item>
    </style>

1

对我来说,情况不同,我使用了一个按钮主题

<style name="ButtonLight_pink" parent="android:Widget.Button">
    <item name="android:background">@drawable/light_pink_btn_default_holo_light</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">64dip</item>
    <item name="android:textColor">@color/tab_background_light_pink</item>
</style>

由于android:textColor是白色的,所以我没有看到任何按钮文本(对话框按钮基本上也是按钮)。更改后修复了这个问题。

0

在学习的过程中,我又花了很多时间来研究样式。需要知道的一件关键事情是,代码比样式更高级。按钮的样式一直无法正常工作,所以我要感谢Ramakrishna Joshi在这篇文章中提供的答案。我对其进行了补充,以便在白天和黑夜主题中都能显示:

private fun AlertDialog.dlgTextColor() {
    val currentNightMode = (resources.configuration.uiMode
            and Configuration.UI_MODE_NIGHT_MASK)
    when (currentNightMode) {
        Configuration.UI_MODE_NIGHT_YES -> {
            this.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.yellow_accent))
            this.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.yellow_accent))
        }
        Configuration.UI_MODE_NIGHT_NO -> {
            this.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.blue_accent))
            this.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.blue_accent))
        }
    }
}

而对它的调用是:

.dlgTextColor()

而且调用该方法确实遵循了在对话框构建器中的.show。


0

这是一个 Kotlin 版本的 @trungdinhtrong 的被接受答案:

val alert = builder.create()
if (button1Text == "Delete") {
    alert.setOnShowListener { dialog ->
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
    }
}

顺便说一下,似乎Android的“积极”和“消极”按钮的概念与“安全”和“破坏性”按钮的概念不兼容。在一个带有取消和删除按钮的对话框中,我认为Android会认为删除是积极按钮,因为它执行了一个操作,但我认为它是一个破坏性按钮,因为它会导致数据丢失。因此,我没有使用样式文件来设置积极和消极按钮的颜色,而是使用这段代码将删除按钮变成红色,即使它是“积极”按钮。


0

快速简单的方法: 在res/values/colors.xml中更改colorAccent颜色,该颜色以十六进制表示,例如#010613是黑色。 再见


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接