PopupWindow和AlertDialog的使用
1. PopupWindow的使用
2.AlertDialog的使用
alert = null;
builder = new AlertDialog.Builder(MainActivity.this);
alert = builder.setIcon(R.mipmap.ic_icon_fish)
.setTitle("系统提示:")
.setMessage("这是一个最普通的AlertDialog,\n带有三个按钮,分别是取消,中立和确定")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了中立按钮~", Toast.LENGTH_SHORT).show();
}
}).create(); //创建AlertDialog对象
alert.show(); //显示对话框
自定义
//初始化Builder
builder = new AlertDialog.Builder(mContext);
//加载自定义的那个View,同时设置下
final LayoutInflater inflater = MainActivity.this.getLayoutInflater();
view_custom = inflater.inflate(R.layout.view_dialog_custom《自己写个布局》, null,false);
builder.setView(view_custom);
builder.setCancelable(false);
alert = builder.create();
//展示
//取消
PopupWindow和AlertDialog的区别:
(1)都可以拥有自己的布局;
(2)PopUPwindow是属于阻塞式的窗口:当窗口弹出的时候,窗口所在的应用程序处于等待状态;
(3)AlertDialog是属于非阻塞式窗口:当弹出对话框的时候,对话框所在的本身的应用程序依然处于运行中的状态;
(4)PopupWindow的覆盖面只是当前的窗口(5)AlertDialog的覆盖面是整个屏幕