1,普通对话框 2,单选对话框 3,多选对话框 4,日期选择对话框 5,时间选择对话框 6,日期选择对话框 7,进度条对话框
普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("标题栏"); builder.setMessage("信息显示区"); //确定按钮,第二个参数为null表示点击后什么也不做 builder.setPositiveButton("确定",null); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogActivity.this, "取消", Toast.LENGTH_SHORT).show(); } }); //点击其他区域dialog不取消 builder.setCancelable(false); //显示对话框 builder.create().show();
单选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("单选对话框"); //builder.setMessage("message"); //这行代码不能加,加了就不显示单选的内容了 final String[] itemNum = {"1", "2", "3"}; //参数1:单选的内容的数组 //参数2:默认选中的是哪一项,-1代表都不选中 //参数3:设置监听器 builder.setSingleChoiceItems(itemNum, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogActivity.this, itemNum[which], Toast.LENGTH_SHORT).show(); //隐藏对话框 dialog.dismiss(); } }); builder.create().show();
多选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("多选对话框"); final String[] itemName = {"选项一","选项二","选项三","选项四"}; boolean[] checkedItem = {false,false,true,true}; //参数1:多选内容数组 //参数2:默认选中哪几项 //参数3:多选对话框监听器 builder.setMultiChoiceItems(itemName, checkedItem, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(DialogActivity.this, isChecked?itemName[which]+"被选中了" :itemName[which]+"未被选中", Toast.LENGTH_SHORT).show(); } }); //需要什么动作,将null给替换成对应的操作即可 builder.setPositiveButton("确定",null); builder.setNegativeButton("取消", null); builder.create().show();
日期选择对话框
//创建Calendar实例对象 Calendar calendar = Calendar.getInstance(); //创建DatePickerDialog并显示 //参数1:上下文 //参数2:日期选择回调 //参数3,4,5 默认显示的年月日 new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(DialogActivity.this, year+"-"+(month+1)+"-"+dayOfMonth , Toast.LENGTH_SHORT).show(); } },calendar.get(Calendar.YEAR) ,calendar.get(Calendar.MONTH) //月份默认是 0-11 ,calendar.get(Calendar.DAY_OF_MONTH)).show();
时间选择对话框
Calendar calendar = Calendar.getInstance(); new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(DialogActivity.this, hourOfDay+":"+minute , Toast.LENGTH_SHORT).show(); } },calendar.get(Calendar.HOUR_OF_DAY) ,calendar.get(Calendar.MINUTE) ,true).show();
进度条对话框
final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("进度"); pd.setMessage("loading..."); //设置进度条为水平风格 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置最大进度值 pd.setMax(100); //创建子线程 模拟下载进度 new Thread(new Runnable() { @Override public void run() { for (int i =0;i<=100;i++) { //休眠50毫秒,模拟下载效果 SystemClock.sleep(50); //设置当前进度 pd.setProgress(i); } //关闭进度条对话框 pd.dismiss(); //google不允许开发者在子线程中修改UI, //所以让Toast代码运行在主线程中 //通过runOnUiThread方法实现 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(DialogActivity.this, "完成", Toast.LENGTH_SHORT).show(); } }); } }).start();//开启线程 //显示进度条对话框 pd.show();