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

非常感谢


当前回答

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

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

结果:

其他回答

你可以这样做:简单的方法

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

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

基本的方法

如果你只是想改变一个活动,在alertDialog.show()后面写下面两行;

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

推荐

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

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

并在AlertDialog中应用主题。构建器

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

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

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

结果:

In your app's theme/style, add the following lines: <item name="android:buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item> <item name="android:buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item> <item name="android:buttonBarNeutralButtonStyle">@style/NeutralButtonStyle</item> Then add the following styles: <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>

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

最简单的解决方案是:

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