Change button color in AlertDialog

后端 未结 16 2357

How can I change the color of the button(s) in an AlertDialog in Android?

16条回答
  •  猫巷女王i
    2020-12-02 22:52

    I think there are developer who wish to extend the AlertDialog class and define the buttons colors withing class definition. For such purpose you can use this code:

    class MyDialog extends AlertDialog {
        public MyDialog(final Context context) {
            super(context); 
            setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);  
                    Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);
    
                    negativeButton.setBackgroundColor(Color.GREEN);
                    positiveButton.setBackgroundColor(Color.RED);
                }
            });
        }
    }
    

提交回复
热议问题