我有很多警报对话框在我的应用程序。这是一个默认的布局,但我添加了积极和消极的按钮对话框。所以按钮得到Android 5的默认文本颜色(绿色)。我试图改变它,但没有成功。知道怎么改变文字的颜色吗?
自定义对话框:
public class MyCustomDialog extends AlertDialog.Builder {
public MyCustomDialog(Context context,String title,String message) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View viewDialog = inflater.inflate(R.layout.dialog_simple, null, false);
TextView titleTextView = (TextView)viewDialog.findViewById(R.id.title);
titleTextView.setText(title);
TextView messageTextView = (TextView)viewDialog.findViewById(R.id.message);
messageTextView.setText(message);
this.setCancelable(false);
this.setView(viewDialog);
} }
创建对话框:
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
...
}
}).show();
那个negativeButton是一个默认的对话框按钮,并采用Android 5 Lollipop的默认绿色。
非常感谢
下面是一种使用样式的自然方法:
如果你的AppTheme继承自Theme。MaterialComponents,那么:
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#f00</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#00f</item>
</style>
如果你的AppTheme继承自Theme。AppCompat:
<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#f00</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#00f</item>
</style>
使用你的AlertDialogTheme在你的AppTheme
<item name="alertDialogTheme">@style/AlertDialogTheme</item>
或者在构造函数中
androidx.appcompat.app.AlertDialog.Builder(context, R.style.AlertDialogTheme)
或者如果你正在使用MaterialAlertDialogBuilder,那么使用
<item name="materialAlertDialogTheme">@style/AlertDialogTheme</item>
使用styles.xml (value)
非常简单的解决方案,改变colorPrimary作为您的选择,它将改变按钮文本的警告框的颜色。
<style name="MyAlertDialogStyle" parent="android:Theme.Material.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">@android:color/white</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@color/black</item>
<!-- Used for the background -->
<item name="android:background">#ffffff</item>
<item name="android:colorPrimary">@color/white</item>
<item name="android:colorAccent">@color/white</item>
<item name="android:windowEnterAnimation">@anim/bottom_left_enter</item>
</style>
替代方案(使用Java)
@SuppressLint("ResourceAsColor")
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog dialog = new AlertDialog.Builder(view.getContext(), R.style.MyAlertDialogStyle)
.setTitle("Royal Frolics")
.setIcon(R.mipmap.ic_launcher)
.setMessage(message)
.setPositiveButton("OK", (dialog1, which) -> {
//do nothing
result.confirm();
}).create();
Objects.requireNonNull(dialog.getWindow()).getAttributes().windowAnimations = R.style.MyAlertDialogStyle;
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(R.color.white);
return true;
}
以下是@trungdinhtrong接受的答案的Kotlin版本:
val alert = builder.create()
if (button1Text == "Delete") {
alert.setOnShowListener { dialog ->
alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
}
}
BTW, it seems like Android's idea of "positive" and "negative" buttons isn't compatible with the idea of "safe" and "destructive" buttons. In a dialog with Cancel and Delete buttons, I think Android would consider Delete the positive button because it performs an action, but I would consider it a destructive button because it leads to data loss. So instead of using the styles file to set positive and negative button colors, I'm using this code to make the Delete button red even though it's the "positive" button.
有两种方法可以更改对话框按钮的颜色。
基本的方法
如果你只是想改变一个活动,在alertDialog.show()后面写下面两行;
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));
推荐
我建议在styles.xml中为AlertDialog添加一个主题,这样就不必在每个活动/对话调用中一次又一次地编写相同的代码。您只需创建一个样式,并在对话框上应用该主题。所以当你想要改变AlertDialog框的颜色时,只要在styles.xml中改变颜色,所有的对话框都会在整个应用程序中更新。
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorPrimary</item>
</style>
并在AlertDialog中应用主题。构建器
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);