弹出窗口有三种实现方式
-
AlertDialog:
-
AlertDialog.Builder => 创建构造器并设置各项属性
-
Builder对象通过create()方法生成AlertDialog对象
-
AlertDialog对象通过show()方法展示出来
-
FragmentManager:
-
getSupportFragmentManager()获取FragmentManager对象
-
manager对象通过beginTransaction()开启事务
-
构造一个弹出框(DialogFragment的子类)
-
弹出框添加到事物中
-
show方法显示出弹出框
-
提交事物
-
PopupWindow:
-
创建PopupWindow
-
创建View对象并设置各项属性
-
PopupWindow.setContentView()
-
设置PopupWindow的展示位置
1. 实现AlertDialog
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c968675d139e9628f589c991f3ec6650.png)
private void showAlertDialog(){
new AlertDialog.Builder(this).setTitle("温馨提示")
.setMessage("确定要退出这个应用程序吗")
.setNegativeButton("取消",null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).