对话框种类:普通对话框,单选对话框,多选对话框,自定义对话框,时间对话框,进度条对话框。
创建过程:创建构建者,设置对话框,创建对话框,显示对话框。
普通对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("你好")
.setMessage("普通对话框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
单选对话框:
final String[] strings = new String[]{"江苏","宿迁","泗阳"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,strings[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}}).create().show();
多选对话框:
final String[] name = new String[]{"你","我","他"};
final boolean[] flag = new boolean[]{true,false,true};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMultiChoiceItems(name, flag, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
flag[i] = b;
Toast.makeText(MainActivity.this,name[i]+":"+b,Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String str="";
for (int i1=0;i1<flag.length;i1++){
if (flag[i1]){
str=str+name[i1]+",";
}
}
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
日期对话框:
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Toast.makeText(MainActivity.this,i+"年"+(i1+1)+"月"+i2+"日",Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
时间对话框:
Calendar calendar = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
Toast.makeText(MainActivity.this,i+"时"+i1+"分",Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
进度条对话框:
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("正在下载");
progressDialog.show();
new Thread(){
@Override
public void run() {
int progress=0;
super.run();
for (int i=0;i<100;i++){
try {
Thread.sleep(200);
progress+=i;
progressDialog.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();