我有很多警报对话框在我的应用程序。这是一个默认的布局,但我添加了积极和消极的按钮对话框。所以按钮得到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的默认绿色。

非常感谢


当前回答

顺便说一句:

按钮的颜色(和整个样式)也取决于当前的主题,当你使用它们的时候可能会有很大的不同

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

or

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

(用第二个更好)

其他回答

我们可以创建扩展函数,并在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))
}

用法:

dialog.show()
dialog.makeButtonTextBlue()

下面是一种使用样式的自然方法:

如果你的AppTheme继承自Theme。MaterialComponents,那么:

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

如果你的AppTheme继承自Theme。AppCompat:

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

使用你的AlertDialogTheme在你的AppTheme

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

或者在构造函数中

androidx.appcompat.app.AlertDialog.Builder(context, R.style.AlertDialogTheme)

或者如果你正在使用MaterialAlertDialogBuilder,那么使用

<item name="materialAlertDialogTheme">@style/AlertDialogTheme</item>
<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更改:

最简单的解决方案是:

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

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

//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()

你必须在onShow()上这样做的原因,不能在你创建对话框后得到那个按钮,因为这个按钮还没有被创建。

我改变了AlertDialog。BUTTON_POSITIVE到AlertDialog。BUTTON_NEGATIVE反映你的问题的变化。虽然“OK”键是否定键有点奇怪。通常是正面按钮。