在AlertDialog中更改按钮文本颜色,可以通过自定义AlertDialog的样式来实现。具体步骤如下:
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:buttonBarButtonStyle">@style/CustomButtonBarButton</item>
</style>
<style name="CustomButtonBarButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/custom_button_text_color</item>
</style>
<color name="custom_button_text_color">#FF0000</color>
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.CustomAlertDialog);
builder.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点击确定按钮的逻辑
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮的逻辑
}
})
.show();
通过以上步骤,就可以在AlertDialog中更改按钮文本的颜色。自定义的AlertDialog样式中,通过设置android:buttonBarButtonStyle属性来指定按钮的样式,然后在CustomButtonBarButton样式中设置android:textColor属性来定义按钮文本的颜色。在代码中创建AlertDialog时,将样式设置为自定义的样式即可生效。
推荐的腾讯云相关产品:腾讯云移动推送(https://cloud.tencent.com/product/tpns)
领取专属 10元无门槛券
手把手带您无忧上云