更改警报对话框的文本颜色

gau*_*ari 26 android android-alertdialog

我有一个弹出窗口,可以在我的应用程序中下载音频指令.我想要做的是我想将"OK"的默认文本颜色更改为蓝色.我试了一下,但它不起作用.这是我的代码:

 private void showDownloadPgmPopup() {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity());
    builder.setTitle("Download instructional audio?");
    builder.setMessage(ParamConstants.AUDIODOWNLOADPERMISSION);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            WwDatabaseHelper.storeSelectedWeekAndDay(getActivity(), mSelectedWeekDataModel);
            goToMoveScreen();
        }
    });
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            AndroidDownloadFileByProgressBarActivity.StartAudioAssetDownload(getActivity());

        }

    }).create();
    // change the text color of download instruction ok button


    final android.app.AlertDialog dialog = builder.show();
    dialog.setOnShowListener( new DialogInterface.OnShowListener() {
                                  @Override
                                  public void onShow(DialogInterface arg0) {
                                      dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#ff5722"));
                                  }
                              });
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
}
Run Code Online (Sandbox Code Playgroud)

但这种变化并没有反映任何人都可以告诉我我做错了什么?

Lin*_*ino 26

您必须在AlertDialog构造函数中提供自定义样式ID:

AlertDialog.Builder(Context context, int themeResId)
Run Code Online (Sandbox Code Playgroud)

和样式文件应该是这样的:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">#0000FF</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Sus*_*ani 14

将您的方法更改为

private void showDownloadPgmPopup() {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity(),R.style.AlertDialog);
...
..
.   
}
Run Code Online (Sandbox Code Playgroud)

在res/values/styles.xml下添加一个新的AlertDialog样式

<style name="AlertDialog" parent="Base.Theme.AppCompat.Light.Dialog">
        <item name="android:textColor">#000000</item>
        <item name="android:textColorPrimary">#595959</item>
        <item name="android:colorAccent">#1b5e20</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

以下是这些更改的屏幕截图

请参见对话框颜色更改


Vah*_*iri 6

尝试致电setTextColor之后show。请参阅以下内容:

show()
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
Run Code Online (Sandbox Code Playgroud)

  • 伟大的!!!谢谢。为我工作。当我使用材料设计主题时,其他答案都不起作用。 (2认同)

Pra*_*are 5

您有两种方法可以做到这一点

  1. 覆盖默认对话框。
//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Exit", 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.parseColor("#f34235"));
    }
}

dialog.show()
Run Code Online (Sandbox Code Playgroud)
  1. 创建自己的custom对话框
// create instance of dialog
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

// get inflater and inflate layour for dialogue 
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);

// now set layout to dialog
dialogBuilder.setView(dialogView);

// create instance like this OR directly mentioned in layout
Button button= (Button) dialogView.findViewById(R.id.label_field);
button.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();

// show dialog
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)