安卓的对话框
系统对话框
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("警告");//设置标题
builder.setMessage("您确定要清空回收站吗?");//设置提示内容
//设置确定按钮的点击事件
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您点击了确定按钮", 0).show();
}
});
//设置取消按钮的点击事件
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您点击了取消按钮", 0).show();
}
});
//对话框中按回退键时调用此方法
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
enterHome();
}
});
//把对话框显示出来
builder.show();
单选对话框
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择课程");//设置标题
//准备数据
final String[] items = {"语文","数学","英语","物理","化学"};
builder.setSingleChoiceItems(items, 1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which], 0).show();
dialog.dismiss();
}
});
//把对话框显示出来
builder.show();
```![这里写图片描述](https://img-blog.csdn.net/20180604152349950?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4ODQ2MDI0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
<div class="se-preview-section-delimiter"></div>
##多选对话框
<div class="se-preview-section-delimiter"></div>
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(“请选择水果”);//设置标题
final String[] items = {“香蕉”,”雪梨”,”西瓜”,”榴莲”,”杨梅”,”菠萝”};
final boolean[] checkedItems = {true,false,true,false,true,false};
builder.setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), items[which], 0).show();
}
});
builder.setPositiveButton(“确定”, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
String fruit = items[i];
sb.append(fruit+” “);
}
}
Toast.makeText(getApplicationContext(), sb, 0).show();
dialog.dismiss();
}
});
//把对话框显示出来
builder.show();
![这里写图片描述](https://img-blog.csdn.net/20180604152451785?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4ODQ2MDI0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
<div class="se-preview-section-delimiter"></div>
##进度条对话框
<div class="se-preview-section-delimiter"></div>
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle(“正在玩命加载中…”);
//将此语句去掉则显示圆形
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
“`