Dialog分三个部分:标题(可以不设置),类容,动作按钮(可以不设置)
Dialog中actions分为三种:
通过builder.setXXXButton("buttonText",onClickedListener)
的方式设置
- 确定按钮(PositiveButton)
builder.setPositiveButton()
- 取消按钮(NegativeButton)
builder.setNegativeButton()
- 中性按钮(NeutralButton)
builder.setNeutralButton()
用于除了确认和取消外的按钮(如了解更多,或者稍后提醒等)
Alert Dialog
1.通过工厂模式来创建AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog alertDialog = builder.create()
设置好dialog的单个部分后,通过builder.create()方法来创建AlertDialog对象
alertDialog.show()
显示AlertDialog
dialog.dismiss();
Dialog消失
alerrtDialog.cancel()
取消AlertDialog
2.设置标题栏
builder.setTitle("DialogTitle");
(title)
builder.setIcon(R.drawable.icon3);
(icon)
builder.setCustomTitle(LayoutInflater.from(getContext()).inflate(R.layout.title, null));
设置自定义布局View的标题
3.设置正文类容和布局
a. 文本确认对话框
builder.setMessage("message");
(content的文本类容)
b.传统单选对话框(一般传统的单选框)
bulider.setItems()
方法添加单选list
c.复选对话框或者Radio单选对话框
builder.setMultiChoiceItems()
复选对话框
builder.setSingleChoiceItems()
Radio单选对话框
4.自定义AlertDialog的Content视图布局
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(dialog, null);
builder.setView(view);//将自定义的View设置为dialog的view
5.自定义AlertDialog的actionButton的属性
AlertDialog alertDialog = builder.create();//获得alertDialog的实例
//为alertDialog设置对话框显示监听时间,当alertDialog调了Show()方法时系统会自动调用该方法
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
AlertDialog alertDialog = ((AlertDialog) dialog);
Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
Log.i("wendy", "button:" + button);
button.setTextColor(getResources().getColor(R.color.colorSelected));
button.setBackgroundColor(getResources().getColor(R.color.colorAccent));
button.setTextSize(20);
}
});//只有当alertDialog调用了show()之后才能通过改方法得到actionButton的实例,来该变button的显示属性