Change button color in AlertDialog

后端 未结 16 2326

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

16条回答
  •  一向
    一向 (楼主)
    2020-12-02 23:09

    Here is how you do it:

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

提交回复
热议问题