如果没有调用Show 方法AlertDialog.getButton(DialogInterface.BUTTON_POSITIVE) 为 null。AlertDialog 的逻辑是 show()方法 中初始化了 AlertDialog 上面的控件,然后展示出来,所以在 show()方法 执行之后才能得到 button。
final AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("sdd")
.setPositiveButton("kk",null).setNegativeButton("取消",null)
.setNeutralButton("dd",null).create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setTextColor(Color.GREEN);
}
});
alertDialog.show();
不用费劲的去改主题了
本文介绍如何在AlertDialog显示后正确设置其按钮的颜色。通过使用setOnShowListener并在onShow方法内部获取按钮,确保按钮已正确初始化。

7248

被折叠的 条评论
为什么被折叠?



