更改AlertDialog中的按钮颜色

mtr*_*mtr 40 alert android dialog colors button

如何AlertDialog在Android中更改按钮的颜色?

小智 67

我就是这样做的.

AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));

customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) {  
        MyActivity.this.finish();
    }  
});

AlertDialog dialog = customBuilder.create();
dialog.show();

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);

if(b != null) {
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
}
Run Code Online (Sandbox Code Playgroud)

我在这里找到了可绘制的

  • 嗨,我也是这样应用Button的BG Image.但如果我应用Image我会在按钮底部获得一些不需要的保证金.如果我使用BG颜色我工作正常.我检查了按钮图像.好的 所以你能为此提出任何解决方案. (2认同)

Bla*_*ght 18

由于大多数人现在可能正在使用DialogFragment,我遇到了一些问题并点击了我的方式通过几个SO答案来解决这些问题.让我发布我目前的解决方案.

我最后设置了自定义drawables的按钮背景已经多次建议.然而,这在onCreateDialog- 的方法中是不可能的DialogFragment.您可以在对话框onStart()onShow-listener中执行此操作,例如in 或(我喜欢的)!但请记住,您需要在更改后使按钮无效.

至于边距:简单地删除Drawable-XML中用于按钮的填充.

DialogFragment中的#onCreateDialog:

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

  // setup your dialog here...

  builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(final DialogInterface dialog, final int which) {
      // do something
    }
  });

  builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(final DialogInterface dialog, final int which) {
      // do something
    }
  });

  final AlertDialog dialog = builder.create();

  dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(final DialogInterface dialog) {
      Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
      Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);

      // this not working because multiplying white background (e.g. Holo Light) has no effect
      //negativeButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);

      final Drawable negativeButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_red);
      final Drawable positiveButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_green);
      if (Build.VERSION.SDK_INT >= 16) {
        negativeButton.setBackground(negativeButtonDrawable);
        positiveButton.setBackground(positiveButtonDrawable);
      } else {
        negativeButton.setBackgroundDrawable(negativeButtonDrawable);
        positiveButton.setBackgroundDrawable(positiveButtonDrawable);
      }

      negativeButton.invalidate();
      positiveButton.invalidate();
    }
  });

  return dialog;
}
Run Code Online (Sandbox Code Playgroud)

按钮的Drawable-XML示例:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true" >
    <shape>
      <gradient
        android:startColor="@color/alert_dialog_button_green_pressed1"
        android:endColor="@color/alert_dialog_button_green_pressed2"
        android:angle="270" />
    </shape>
  </item>

  <item android:state_focused="true" >
    <shape>
      <gradient
        android:endColor="@color/alert_dialog_button_green_focused1"
        android:startColor="@color/alert_dialog_button_green_focused2"
        android:angle="270" />
    </shape>
  </item>

  <item>
    <shape>
      <gradient
        android:endColor="@color/alert_dialog_button_green1"
        android:startColor="@color/alert_dialog_button_green2"
        android:angle="270" />
    </shape>
  </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

不要忘记定义颜色res\values\colors.xml,例如像这样(我不想一个梯度,因此颜色1和2是相同的):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="alert_dialog_button_green1">#b4099930</color>
  <color name="alert_dialog_button_green2">#b4099930</color>
  <color name="alert_dialog_button_green_focused1">#96099930</color>
  <color name="alert_dialog_button_green_focused2">#96099930</color>
  <color name="alert_dialog_button_green_pressed1">#96099930</color>
  <color name="alert_dialog_button_green_pressed2">#96099930</color>
</resources>
Run Code Online (Sandbox Code Playgroud)


Ami*_*rma 13

我通过这段代码完成了它可能对你有所帮助:

AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
        builder1.setCancelable(true);
     builder1.setTitle("abc");
      builder1.setMessage("abcdefg");
      builder1.setInverseBackgroundForced(true);
     builder1.setPositiveButton("Yes",
             new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
             dialog.cancel();
         }
     }); 

     builder1.setNegativeButton("No",
             new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
             dialog.cancel();
         }
     });

     AlertDialog alert11 = builder1.create();
     alert11.show(); 

     Button buttonbackground = alert11.getButton(DialogInterface.BUTTON_NEGATIVE); 
     buttonbackground.setBackgroundColor(Color.BLUE); 

     Button buttonbackground1 = alert11.getButton(DialogInterface.BUTTON_POSITIVE); 
     buttonbackground1.setBackgroundColor(Color.BLUE);
Run Code Online (Sandbox Code Playgroud)


Fab*_*tel 10

我想用主题而不是额外的代码来解决这个问题,因为在styles.xml中拥有所有与样式相关的东西对我来说感觉更干净.我所做的是基于Arade的答案和另一个问题:

<style name="AlertDialogDanger" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/error</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这将更改您使用样式创建的任何警报对话框的按钮文本的颜色AlertDialogDanger.为此:

    new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogDanger))
            .setMessage("Really delete?")
            .setPositiveButton("Delete", null)
            .setNegativeButton("Cancel", null)
            .create().show();
Run Code Online (Sandbox Code Playgroud)


小智 9

这是一些例子:

AlertDialog.Builder b = new AlertDialog.Builder(all.this);

b.setMessage("r u wan't 2 exit");
b.setCancelable(false);

b.setNegativeButton("no", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();    
    }
});

b.setPositiveButton("yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Intent i=new Intent(getBaseContext(), s.class);
        startActivity(i);
    }
});

AlertDialog a=b.create();

a.show();

Button bq = a.getButton(DialogInterface.BUTTON_NEGATIVE);  
bq.setBackgroundColor(Color.BLUE);
Run Code Online (Sandbox Code Playgroud)


Abd*_*wan 7

我们可以使用样式更改警报对话框按钮文本颜色。

 AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.yourDialog);
    dialog.setTitle(R.string.title);
    dialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //code here 
        }
    });
    dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //do here 
        }
    });

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

样式文件

<style name="yourDialog" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:colorAccent">@color/themeColor</item>
    <item name="android:colorPrimary">@color/themeColor</item>

</style>
Run Code Online (Sandbox Code Playgroud)


Zar*_*rah -1

您指的是中性、积极和消极按钮吗?或者您包含在布局中的按钮?

如果您指的是前者,那么可以。查看本教程中的自定义按钮部分。您基本上需要一个 XML 文件来告诉您的按钮针对每个状态更改使用哪种可绘制/颜色。然后您可以将此 XML 文件设置为按钮的背景。