更改 AlertDialog 中确定按钮的颜色
Changing color of OK button in AlertDialog
我正在尝试更改 Android 中 AlertDialog
中正 button
的颜色。在我的 S8 上,按钮本来是绿色的,但是当我尝试更改它时,无论我选择什么颜色,它都会变成亮紫色。(我尝试了许多不同深浅的蓝色甚至粉色来测试)
我正在更改颜色:
dialog.getButton(DialogInterface.BUTTON_POSITIVE).textColor = R.color.color_blue
我在 dialog.show()
之后调用它。
试试下面的方法,你可能会得到你的解决方案
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);
我宁愿使用 theme
来控制对话框的外观。你的情况
R.color.color_blue 是资源 ID。您必须将其转换为 color。例如
dialog.getButton(DialogInterface.BUTTON_POSITIVE).textColor = ContextCompat.getColor(this, R.color.color_blue)
你可以先尝试创建AlertDialog
对象,然后用它来设置改变按钮的颜色然后显示。 (请注意,在 builder
对象上我们调用 create()
而不是调用 show()
来获取 AlertDialog
对象:
//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", 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_I_WANT);
}
});
dialog.show()
您必须在 onShow()
上执行此操作并且不能在创建对话框后才获取该按钮的原因是该按钮尚未创建。
我将 AlertDialog.BUTTON_POSITIVE
更改为 AlertDialog.BUTTON_NEGATIVE
以反映您问题中的更改。尽管 "OK" 按钮是一个否定按钮很奇怪。一般是正键。
您可以为警报对话框设置自定义主题。
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorPrimary">#FFFFFF</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:colorAccent">#FFFFFF</item>
<item name="colorPrimaryDark">#FFFFFF</item>
</style>
根据您的选择设置颜色。
我正在尝试更改 Android 中 AlertDialog
中正 button
的颜色。在我的 S8 上,按钮本来是绿色的,但是当我尝试更改它时,无论我选择什么颜色,它都会变成亮紫色。(我尝试了许多不同深浅的蓝色甚至粉色来测试)
我正在更改颜色:
dialog.getButton(DialogInterface.BUTTON_POSITIVE).textColor = R.color.color_blue
我在 dialog.show()
之后调用它。
试试下面的方法,你可能会得到你的解决方案
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);
我宁愿使用 theme
来控制对话框的外观。你的情况
R.color.color_blue 是资源 ID。您必须将其转换为 color。例如
dialog.getButton(DialogInterface.BUTTON_POSITIVE).textColor = ContextCompat.getColor(this, R.color.color_blue)
你可以先尝试创建AlertDialog
对象,然后用它来设置改变按钮的颜色然后显示。 (请注意,在 builder
对象上我们调用 create()
而不是调用 show()
来获取 AlertDialog
对象:
//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", 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_I_WANT);
}
});
dialog.show()
您必须在 onShow()
上执行此操作并且不能在创建对话框后才获取该按钮的原因是该按钮尚未创建。
我将 AlertDialog.BUTTON_POSITIVE
更改为 AlertDialog.BUTTON_NEGATIVE
以反映您问题中的更改。尽管 "OK" 按钮是一个否定按钮很奇怪。一般是正键。
您可以为警报对话框设置自定义主题。
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorPrimary">#FFFFFF</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:colorAccent">#FFFFFF</item>
<item name="colorPrimaryDark">#FFFFFF</item>
</style>
根据您的选择设置颜色。