在AlertDialog中,如果要自定义按钮的颜色,可以采用如下代码:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Message")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
positiveButton.setTextColor(getResources().getColor(R.color.colorAccent));
Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
negativeButton.setTextColor(getResources().getColor(R.color.colorPrimary));
其中,首先需要创建一个AlertDialog.Builder实例,然后通过设置其按钮的颜色即可。使用getButton()方法来获取对应的按钮,然后通过setTextColor()方法来设置颜色。
需要注意的是,颜色值需要从资源文件中获取,例如从color.xml文件中获取颜色值,如上面的代码所示。