Dialog dialog
1. Common dialog
1. Common dialog
2. Single selection dialog
3. Multiple selection dialog
4. Customize the dialog
5. Horizontal progress bar dialog
6. Circular progress bar dialog
7. Date selection dialog
8. Time selection dialog
1. Commonly used dialog boxes
Dialog box | class | special |
---|---|---|
ordinary | AlertDialog.Builder() | |
Single choice | AlertDialog.Builder() | setSingleChoiceItems () |
Multiple choice | AlertDialog.Builder() | setMultiChoiceItems() |
date | DatePickerDialog | new DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day) |
time | TimePickerDialog | new TimePickerDialog(context, TimePickerDialog.OnTimeSetListener, hour, minute, 24 hexadecimal) |
Level | ProgressDialog | setStyle(ProgressDialog.STYLE_HORIZONTAL) |
Circle | ProgressDialog | setStyle(ProgressDialog.STYLE_SPINNER) |
customize | AlertDialog.Builder() | setView() |
1. Common dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);//创建构造者
builder.setIcon(R.mipmap.zx);//设置图标
builder.setTitle("大帅哥"); //设置标题
builder.setMessage("我是1702C申德丰"); //设置内容
//设置确定按钮
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了ok", Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击取消了", Toast.LENGTH_SHORT);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
2. Radio button dialog
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("我是最帅的吗");
builder2.setPositiveButton("好的", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了好的", Toast.LENGTH_SHORT).show();
}
});
builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
final String[] s=new String []{
"帕加尼","布加迪威龙","法拉利拉法"};
builder2.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选中"+s[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog2 = builder2.create();
alertDialog2.show();
3. Multiple selection dialog
final String[] items={
"申德丰","王鹏","张泽涛","杨子豪","常帅"};
final boolean[] flags=new boolean[]{
true,true,true,true,false};
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setIcon(R.mipmap.zx);
builder3.setTitle("通讯人名");
builder3.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0;i<flags.length;i++){
if(flags[i]){
Toast.makeText(MainActivity.this, "这个人是:"+items[i], Toast.LENGTH_SHORT).show();
}
}
}
});
builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
builder3.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which] = isChecked;
}
});
AlertDialog alertDialog3 = builder3.create();
alertDialog3.show();
4. Customize the dialog
Custom layout
View view= LayoutInflater.from(this).inflate(R.layout.layout1,null);
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);//创建构造者
builder1.setIcon(R.mipmap.zx);//设置图标
builder1.setTitle("大帅哥"); //设置标题
//设置确定按钮
// builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(MainActivity.this, "点击了ok", Toast.LENGTH_SHORT).show();
// }
// });
// //设置取消按钮
// builder1.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(MainActivity.this, "点击取消了", Toast.LENGTH_SHORT);
// }
// });
Button button = view.findViewById(R.id.btn_close);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog1.dismiss();
}
});
builder1.setView(view);
alertDialog1 = builder1.create();
alertDialog1.show();
5. Horizontal progress bar dialog
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setMessage("正在飞速下载");
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if(progress==100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(progress+=20);
}
},0,1000);
6. Circular progress bar dialog
final ProgressDialog progressDialog1 = new ProgressDialog(this);
progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog1.setMax(100);
progressDialog1.setMessage("正在飞速下载");
progressDialog1.show();
final Timer timer1 = new Timer();
timer1.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if(progress==100){
progressDialog1.dismiss();
timer1.cancel();
}
progressDialog1.setProgress(progress+=20);
}
},0,1000);
7. Date selection dialog
Calendar instance = Calendar.getInstance();
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
8. Time selection dialog
Calendar instance1 = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},instance1.get(Calendar.HOUR),instance1.get(Calendar.MINUTE),true).show();