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

时间:2022-11-20 23:06:20

I have many alert dialogs in my app. It is a default layout but I am adding positive and negative buttons to the dialog. So the buttons get the default text color of Android 5 (green). I tried to changed it without success. Any idea how to change that text color?

我的应用程序中有很多警报对话框。这是默认布局,但我在对话框中添加正负按钮。因此按钮获得Android 5(绿色)的默认文本颜色。我试图改变它但没有成功。知道如何改变文字颜色吗?

My Custom dialog:

My Custom对话框:

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);

    } }

Creating the dialog:

创建对话框:

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

That negativeButton is a default dialog button and takes the default green color from Android 5 Lollipop.

该negativeButton是一个默认对话框按钮,采用Android 5 Lollipop的默认绿色。

Many thanks

非常感谢

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

9 个解决方案

#1


113  

You can try to create the AlertDialog object first, and then use it to set up to change the color of the button and then show it. (Note that on builder object instead of calling show() we call create() to get the AlertDialog object:

您可以先尝试创建AlertDialog对象,然后使用它来设置更改按钮的颜色然后显示它。 (注意,在构建器对象上而不是调用show()时,我们调用create()来获取AlertDialog对象:

//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage); 
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ...
                }

            }).create();

//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
    @Override
    public void onShow(DialogInterface arg0) {
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_I_WANT);
    }
});

dialog.show()

The reason you have to do it on onShow() and cannot just get that button after you create your dialog is that the button would not have been created yet.

您必须在onShow()上执行此操作并且在创建对话框后不能只获取该按钮的原因是该按钮尚未创建。

I changed AlertDialog.BUTTON_POSITIVE to AlertDialog.BUTTON_NEGATIVE to reflect the change in your question. Although it is odd that "OK" button would be a negative button. Usually it is the positive button.

我将AlertDialog.BUTTON_POSITIVE更改为AlertDialog.BUTTON_NEGATIVE以反映您的问题中的更改。虽然很奇怪“OK”按钮会是一个负面按钮。通常它是正面按钮。

#2


93  

The color of the buttons and other text can also be changed via theme:

按钮和其他文本的颜色也可以通过主题更改:

values-21/styles.xml

值-21 / styles.xml

<style name="AppTheme" parent="...">
  ...
  <item name="android:timePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:datePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
</style>

<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
  <item name="android:colorPrimary">#00397F</item>
  <item name="android:colorAccent">#0AAEEF</item>
</style>

The result:

结果:

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

#3


81  

Here's a natural way to do it through styles:

这是通过样式执行此操作的自然方式:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#f00</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#00f</item>
</style>

Set AlertDialogTheme through this attribute in your AppTheme

在AppTheme中通过此属性设置AlertDialogTheme

<item name="alertDialogTheme">@style/AlertDialogTheme</item>

or via constructor in builder new AlertDialog.Builder(context, R.style.AlertDialogTheme)

或者通过构建器中的构造函数new AlertDialog.Builder(context,R.style.AlertDialogTheme)

#4


60  

The simpliest solution is:

最简单的解决方案是:

dialog.show(); //Only after .show() was called
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);

#5


6  

If you want to change buttons text color (positive, negative, neutral) just add to your custom dialog style:

如果要更改按钮文本颜色(正面,负面,中性),只需添加到自定义对话框样式:

<item name="colorAccent">@color/accent_color</item>

So, your dialog style must looks like this:

因此,您的对话框样式必须如下所示:

<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@android:color/black</item>
    <item name="colorAccent">@color/topeka_accent</item>
</style>

#6


5  

<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>

The color of the buttons and other text can also be changed using appcompat :

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

#7


4  

Just as a side note:

正如旁注:

The colors of the buttons (and the whole style) also depend on the current theme which can be rather different when you use

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

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

or

要么

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

(better use the second one)

(更好地使用第二个)

#8


4  

There are two ways to change the dialog button color.

有两种方法可以更改对话框按钮颜色。

Basic Way

基本方式

If you just want to change in an activity, write the below two lines after alertDialog.show();

如果您只想更改活动,请在alertDialog.show()之后写下以下两行;

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));

Recommended

推荐的

I'll recommend adding a theme for AlertDialog in styles.xml so you don't have to write the same code again and again in each activity/dialog call. You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles.xml and all the dialog boxes will be updated in the whole application.

我建议在styles.xml中为AlertDialog添加一个主题,这样你就不必在每个活动/对话框调用中反复写入相同的代码。您可以只创建一个样式并在对话框中应用该主题。因此,每当您想要更改AlertDialog框的颜色时,只需更改styles.xml中的颜色,所有对话框都将在整个应用程序中更新。

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item>
</style>

And apply the theme in AlertDialog.Builder

并在AlertDialog.Builder中应用主题

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);

#9


0  

for me it was different, i used a button theme

对我来说它是不同的,我使用按钮主题

<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>

and because

因为

android:textColor

机器人:文字颜色

was white there… i didn't see any button text (Dialog buttons are basically buttons too). there we go, changed it, fixed it.

那里是白色的...我没有看到任何按钮文字(对话框按钮基本上也是按钮)。我们去,改变它,修复它。

#1


113  

You can try to create the AlertDialog object first, and then use it to set up to change the color of the button and then show it. (Note that on builder object instead of calling show() we call create() to get the AlertDialog object:

您可以先尝试创建AlertDialog对象,然后使用它来设置更改按钮的颜色然后显示它。 (注意,在构建器对象上而不是调用show()时,我们调用create()来获取AlertDialog对象:

//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage); 
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ...
                }

            }).create();

//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
    @Override
    public void onShow(DialogInterface arg0) {
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_I_WANT);
    }
});

dialog.show()

The reason you have to do it on onShow() and cannot just get that button after you create your dialog is that the button would not have been created yet.

您必须在onShow()上执行此操作并且在创建对话框后不能只获取该按钮的原因是该按钮尚未创建。

I changed AlertDialog.BUTTON_POSITIVE to AlertDialog.BUTTON_NEGATIVE to reflect the change in your question. Although it is odd that "OK" button would be a negative button. Usually it is the positive button.

我将AlertDialog.BUTTON_POSITIVE更改为AlertDialog.BUTTON_NEGATIVE以反映您的问题中的更改。虽然很奇怪“OK”按钮会是一个负面按钮。通常它是正面按钮。

#2


93  

The color of the buttons and other text can also be changed via theme:

按钮和其他文本的颜色也可以通过主题更改:

values-21/styles.xml

值-21 / styles.xml

<style name="AppTheme" parent="...">
  ...
  <item name="android:timePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:datePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
</style>

<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
  <item name="android:colorPrimary">#00397F</item>
  <item name="android:colorAccent">#0AAEEF</item>
</style>

The result:

结果:

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

#3


81  

Here's a natural way to do it through styles:

这是通过样式执行此操作的自然方式:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#f00</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#00f</item>
</style>

Set AlertDialogTheme through this attribute in your AppTheme

在AppTheme中通过此属性设置AlertDialogTheme

<item name="alertDialogTheme">@style/AlertDialogTheme</item>

or via constructor in builder new AlertDialog.Builder(context, R.style.AlertDialogTheme)

或者通过构建器中的构造函数new AlertDialog.Builder(context,R.style.AlertDialogTheme)

#4


60  

The simpliest solution is:

最简单的解决方案是:

dialog.show(); //Only after .show() was called
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);

#5


6  

If you want to change buttons text color (positive, negative, neutral) just add to your custom dialog style:

如果要更改按钮文本颜色(正面,负面,中性),只需添加到自定义对话框样式:

<item name="colorAccent">@color/accent_color</item>

So, your dialog style must looks like this:

因此,您的对话框样式必须如下所示:

<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@android:color/black</item>
    <item name="colorAccent">@color/topeka_accent</item>
</style>

#6


5  

<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>

The color of the buttons and other text can also be changed using appcompat :

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

#7


4  

Just as a side note:

正如旁注:

The colors of the buttons (and the whole style) also depend on the current theme which can be rather different when you use

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

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

or

要么

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

(better use the second one)

(更好地使用第二个)

#8


4  

There are two ways to change the dialog button color.

有两种方法可以更改对话框按钮颜色。

Basic Way

基本方式

If you just want to change in an activity, write the below two lines after alertDialog.show();

如果您只想更改活动,请在alertDialog.show()之后写下以下两行;

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));

Recommended

推荐的

I'll recommend adding a theme for AlertDialog in styles.xml so you don't have to write the same code again and again in each activity/dialog call. You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles.xml and all the dialog boxes will be updated in the whole application.

我建议在styles.xml中为AlertDialog添加一个主题,这样你就不必在每个活动/对话框调用中反复写入相同的代码。您可以只创建一个样式并在对话框中应用该主题。因此,每当您想要更改AlertDialog框的颜色时,只需更改styles.xml中的颜色,所有对话框都将在整个应用程序中更新。

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item>
</style>

And apply the theme in AlertDialog.Builder

并在AlertDialog.Builder中应用主题

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);

#9


0  

for me it was different, i used a button theme

对我来说它是不同的,我使用按钮主题

<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>

and because

因为

android:textColor

机器人:文字颜色

was white there… i didn't see any button text (Dialog buttons are basically buttons too). there we go, changed it, fixed it.

那里是白色的...我没有看到任何按钮文字(对话框按钮基本上也是按钮)。我们去,改变它,修复它。