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

FOM*_*per 127 android textcolor android-layout android-alertdialog android-5.0-lollipop

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

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

    } }
Run Code Online (Sandbox Code Playgroud)

创建对话框:

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

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

非常感谢

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

Ale*_*yev 214

以下是使用样式执行此操作的自然方法:

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#f00</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#00f</item>
</style>
Run Code Online (Sandbox Code Playgroud)

AppTheme在您的中设置此属性Theme.MaterialComponents

<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.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>
Run Code Online (Sandbox Code Playgroud)

或者通过构建器中的构造函数 AppTheme

  • 我不得不将buttonBarNegativeButtonStyle更改为android:buttonBarNegativeButtonStyle,将buttonBarPositiveButtonStyle更改为android:buttonBarPositiveButtonStyle.然后它工作(API 21+). (29认同)
  • 请记住使用android.support.v7.app.AlertDialog而不是android.app.AlertDialog.废话错误花了我2个小时 (16认同)
  • 使用新的材质库`com.google.android.material:material:1.0.0-beta01`时,此方法不起作用,而我正在使用Theme.MaterialComponents.Light.Dialog.Alert (3认同)
  • 我必须将AlertDialogTheme的父级更改为“ Base.Theme.AppCompat.Light.Dialog.Alert”。并删除buttonBarNegativeButtonStyle和buttonBarPositiveButtonStyle。还要在AlertDialogTheme中添加&lt;item name =“ colorAccent”&gt; @ color / dashboard_red_color &lt;/ item&gt;。nd工作完美。 (2认同)
  • @LX 更新了答案以包含材料组件主题 (2认同)
  • 对于 Android 的 Material 组件,“&lt;item name="alertDialogTheme"&gt;@style/AlertDialogTheme&lt;/item&gt;”应为“&lt;item name="materialAlertDialogTheme"&gt;@style/AlertDialogTheme&lt;/item&gt;”。 (2认同)

tru*_*ong 164

您可以先尝试创建AlertDialog对象,然后使用它来设置更改按钮的颜色然后显示它.(注意,在builder对象而不是调用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()
Run Code Online (Sandbox Code Playgroud)

您必须onShow()在创建对话框后执行此操作并且无法获得该按钮的原因是该按钮尚未创建.

我改变AlertDialog.BUTTON_POSITIVEAlertDialog.BUTTON_NEGATIVE以反映你的问题的变化.虽然很奇怪"OK"按钮会是一个负面按钮.通常它是正面按钮.

  • 该方法在AlertDialog类中,而不是Builder类.因此,您可以使用Builder.create()来返回AlertDialog类,而不是调用Builder.show().然后设置监听器,然后在AlertDialog对象上调用show() (5认同)
  • 但必须有另一种方法来做到这一点.看起来它是主题的颜色,我们可以通过主题/风格改变它吗? (3认同)

pec*_*eps 114

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

值-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>
Run Code Online (Sandbox Code Playgroud)

结果:

对话 日期选择器

  • 为了让这个在我的项目中工作,我不得不从`android:alertDialogTheme`和`android:colorAccent`中删除`android:`部分. (12认同)
  • 我喜欢这种方法,但我觉得http://stackoverflow.com/a/29810469/2291的答案是一种稍微简洁的方法. (4认同)
  • 但android:colorAccent是更改复选框和按钮.但是如何只改变按钮textcolor,而不是复选框.或者其他方式...... (3认同)
  • 在值上使用android:前缀取决于您将styles.xml,值或值-vXX中的位置放在哪里 (2认同)

Art*_*miy 85

最简单的解决方案是:

dialog.show(); //Only after .show() was called
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);
Run Code Online (Sandbox Code Playgroud)

  • 这些字段不应该从非静态变量中引用,应该是`AlertDialog.BUTTON_NEGATIVE`等. (4认同)
  • 虽然这个解决方案可能有效,但从逻辑上讲它是有缺陷的。这里发生的事情是您首先显示对话框,然后更改其外观。取决于底层实现(可以随着时间的推移而改变)和设备的性能,理论上你可以看到一个“闪烁”,用户看到一个对话框出现,然后快速改变它的外观。 (2认同)

Has*_*mil 23

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

基本方式

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

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));
Run Code Online (Sandbox Code Playgroud)

推荐的

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

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并将主题应用于 AlertDialog.Builder

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是最干净的,同时仍然是正确的。 (2认同)

Joe*_*ler 13

  1. 在您应用的主题/样式中,添加以下几行:

    <item name="android:buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="android:buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    <item name="android:buttonBarNeutralButtonStyle">@style/NeutralButtonStyle</item>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后添加以下样式:

    <style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/red</item>
    </style>
    
    <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/red</item>
    </style>
    
    <style name="NeutralButtonStyle" 
    parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">#00f</item>
    </style>
    
    Run Code Online (Sandbox Code Playgroud)

使用此方法无需在 AlertDialog 构建器中设置主题。


gal*_*cin 11

Kotlin 2020:非常简单的方法

使用后dialog.show()

dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(requireContext(), R.color.yourColor))
Run Code Online (Sandbox Code Playgroud)


小智 9

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

<item name="colorAccent">@color/accent_color</item>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)


Ram*_*shi 7

我们可以创建扩展函数并在dialog.show()之后调用扩展函数来自定义警报对话框按钮的颜色。

fun AlertDialog.makeButtonTextBlue() {
this.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.blue))
this.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.blue))
}
Run Code Online (Sandbox Code Playgroud)

用法:

dialog.show()
dialog.makeButtonTextBlue()
Run Code Online (Sandbox Code Playgroud)


Ara*_*ade 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>
Run Code Online (Sandbox Code Playgroud)

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


MrG*_*MrG 6

这是你的方法:简单的方法

// Initializing a new alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.message);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        doAction();
    }
});
builder.setNegativeButton(R.string.cancel, null);

// Create the alert dialog and change Buttons colour
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface arg0) {
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.red));
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.blue));
        //dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.black));
    }
});
dialog.show();
Run Code Online (Sandbox Code Playgroud)


Sha*_*651 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>
Run Code Online (Sandbox Code Playgroud)

替代方案(使用 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;

            }
Run Code Online (Sandbox Code Playgroud)