定义警报对话框的按钮样式

时间:2019-03-15 11:13:20

标签: java android modal-dialog material-design

我正在使用Android应用程序。我需要自定义“警报对话框”按钮,因为它以未指定的方式向我显示按钮。

enter image description here

调用警报对话框的代码是:

 new AlertDialog.Builder(context,R.style.dialog_theme)
                .setTitle(R.string.dialog_title_avviso)
                .setMessage(messageResId)
                .setPositiveButton(R.string.dialog_button_si, (dialog, which) -> listener.onResponse(dialog, DialogResponse.OK))
                .setNegativeButton(R.string.dialog_button_no, (dialog, which) -> listener.onResponse(dialog, DialogResponse.CANCEL))
                .show();

style.xml中,我定义了以下样式:

<style name="dialog_theme" parent="Theme.AppCompat.Light.Dialog.Alert">
  <item name="android:windowTitleStyle">@style/AlertDialogTitleStyle</item>
</style>

我想将对话设置为以下模式:

enter image description here

有什么想法吗?谢谢

1 个答案:

答案 0 :(得分:0)

首先创建警报对话框对象,然后使用它来更改按钮内文本的颜色。

CustomDialog builder = new CustomDialog(getActivity(), "Try Again", errorMessage); 
    AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ...
                    }

                }).create();


    dialog.setOnShowListener( new OnShowListener() {
        @Override
        public void onShow(DialogInterface arg0) {
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR.BLUE);
        }
    });

    dialog.show()
相关问题