如何更改默认对话框按钮的文字颜色在android系统5 [英] How can I change default dialog button text color in android 5

查看:2182
本文介绍了如何更改默认对话框按钮的文字颜色在android系统5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序许多警报对话框。这是一个默认的布局,但我加入正面和负面的按钮对话框。所以按键得到Android的5(绿色)的默认文本颜色。我试图改变它没有成功。任何想法如何更改文本颜色?

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?

我的自定义对话框:

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棒棒堂默认的绿色。

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

非常感谢

推荐答案

您可以尝试先创建 AlertDialog 对象,然后用它来建立改变按钮的颜色,然后显示它(请注意,建设者对象,而不是调用显示()我们称之为创建()来获得 AlertDialog 对象:

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:

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

我改变AlertDialog.BUTTON_POSITIVE到AlertDialog.BUTTON_NEGATIVE以反映你的问题的变化。虽然它是奇怪的是,OK按钮将是一个负按钮。通常它是积极的按钮。

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.

这篇关于如何更改默认对话框按钮的文字颜色在android系统5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆