更改对话框按钮颜色 [英] Change dialog button color

本文介绍了更改对话框按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以更改默认对话框按钮的颜色,或者我需要为此做自定义对话框吗?

这是我的对话:

private void removeItem(final int position) {/** 创建对话框,询问用户是否确定要从列表中删除名称 **/AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("删除玩家");builder.setIcon(R.mipmap.ic_launcher);builder.setMessage("是否要删除玩家:"" + mNameList.get(position).getText1() + ""?").setCancelable(false)/** 如果用户单击确定"按钮,则从列表中删除播放器并保存更改 **/.setPositiveButton("OK", new DialogInterface.OnClickListener() {@覆盖public void onClick(DialogInterface dialogInterface, int i) {mNameList.remove(位置);mAdapter.notifyItemRemoved(position);保存数据();}})/** 如果用户点击取消"按钮,名称不会从列表中删除 **/.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@覆盖public void onClick(DialogInterface dialogInterface, int i) {dialogInterface.dismiss();}});/** 创建对话框 **/AlertDialog 警报 = builder.create();警报.显示();}

解决方案

您可以使用

Is there any way how to change default dialog buttons colour, or do I need to do custom dialog for that?

This is my dialog:

private void removeItem(final int position) {
    /** Create dialog which ask if user is sure about delete name from list **/
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Delete player");
    builder.setIcon(R.mipmap.ic_launcher);
    builder.setMessage("Do you want to delete player: "" + mNameList.get(position).getText1() + ""?")
            .setCancelable(false)

            /** If user click "ok" button, delete player from list and save changes **/
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mNameList.remove(position);
                    mAdapter.notifyItemRemoved(position);
                    saveData();
                }
            })

            /** If user click "cancel" button, name won't delete from list **/
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });

    /** Create Dialog **/
    AlertDialog alert = builder.create();
    alert.show();
}

解决方案

You can use the MaterialAlertDialogBuilder provided by the Material Components library which allows the creation of a Material AlertDialog.

Just use:

   new MaterialAlertDialogBuilder(MainActivity.this,
       R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Dialog")
              .setMessage("Lorem ipsum dolor ....")
              .setPositiveButton("Ok", /* listener = */ null)
              .setNegativeButton("Cancel", /* listener = */ null)
              .show();

Then define your custom style, using the buttonBarPositiveButtonStyle and buttonBarNegativeButtonStyle attributes:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
     <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
     <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
     <item name="buttonBarNeutralButtonStyle">....</item>
  </style>


  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">#00f</item>
  </style>

  <style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/colorAccent</item>
    <item name="backgroundTint">@color/secondaryColor</item>
  </style>

这篇关于更改对话框按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆