Change button color in AlertDialog

后端 未结 16 2359

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

16条回答
  •  天涯浪人
    2020-12-02 22:52

    if you are using DialogFragment ( android.app.DialogFragment ) then you can overwrite onStart method to get handle of all the buttons (Positive, Negative and Neutral).

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog,
                                                null);
    
        builder.setTitle(getLocalizedString("edit_event"))
                .setView(eventEditDialogView)
                .setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
                .setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
        return builder.create();
    }
    
     @Override
        public void onStart() {
            super.onStart();
        Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
        positive.setTextColor(Color.BLACK);
        positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
    }
    

    All the above solutions will work with AlertDialog or Dialog created on same activity or Fragment but not on DialogFragment created separately.

提交回复
热议问题