一、常用对话框
1.普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("确认吗");
builder.setMessage("选择吐司");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了OK", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了no", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
2.单选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("你出生的城市");
final String[] strings={"山西","河南","山东"};
builder.setSingleChoiceItems(strings, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你选中"+strings[i], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了OK", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了no", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
3.多选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("你去过的城市");
final String[] strings = {"山西","河南","山东"};
final boolean[] booleans ={true,false,false};
builder.setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
booleans[i] = b;
}
});
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
for (int j = 0; j < booleans.length; j++) {
if (booleans[j]){
Toast.makeText(MainActivity.this, "你选择的"+strings[j], Toast.LENGTH_SHORT).show();
}
}
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了no", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
4.水平对话框&圆形对话框
水平和圆形只需更改进度条样式即可
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("正在下载");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i = 0;
@Override
public void run() {
i+=10;
progressDialog.setProgress(i);
if (i > 100){
timer.cancel();
progressDialog.dismiss();
}
}
},1000,1000);
5.日期选择对话框
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+"月"+i2+"日", Toast.LENGTH_SHORT).show();
}
},calendar.get(calendar.YEAR),calendar.get(calendar.MONTH),calendar.get(calendar.DAY_OF_MONTH)).show();
6.时间选择对话框
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),calendar.get(calendar.MINUTE),true).show();
7.简单自定义对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("动物");
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.diglogmine, null);
builder.setView(inflate);
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了OK", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了no", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();