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

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

问题描述

我的应用程序中有很多提醒对话框。它是一个默认布局,但我正在添加正负按钮到对话框。所以按钮可以获得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);
查看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();

该负面按钮是默认的对话框按钮,并从Android 5 Lollipop获取默认的绿色。



非常感谢



解决方案

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

  // 1。创建对话框对话框
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。现在设置更改按钮的颜色
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按钮将是一个负面的按钮,通常是正按钮。 p>

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?

My Custom dialog:

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.

Many thanks

解决方案

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.

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天全站免登陆