Android编程知识
AlertDialog中的取消、确定按钮显示不出问题
2023-05-24 216 0
简介 AlertDialog中的取消、确定按钮显示不出问题
在安卓开发工作中,成千上万不同的机型中适配是很有必要的事情,今日为大家解决AlertDialog中的取消、确定按钮显示不到问题。
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("網絡提示");
builder.setMessage(msg);
builder.setNegativeButton("取消", null);
builder.setPositiveButton("繼續下載", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//todo 你的业务
}
});
AlertDialog dialog = builder.create();
dialog.show();
上面是正常创建和显示一个AlertDialog,但单单这样处理有部分系统(忘记了什么版本以上了)就显示不出"取消"和"继续下载"两个按钮的字体了,我发现其实这两个按钮一直都是有的,可能是字体颜色跟背景颜色一样了,所以就看不见。所以解决版本就是改变这两个按钮的颜色。
Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
btnPos.setTextColor(Color.RED);
btnNeg.setTextColor(Color.RED);
上面代码必须要在dialog.show();后面调用才会生效。