一、确定取消选择对话框
public void simple_dialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("普通对话框");
builder.setMessage("我是来自远方的你");
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, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/394ed37ca0bd4b6835c9d687726958bd.png)
二、单选对话框
public void simple_dialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("单选对话框");
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, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
final String[] items = {"0-16岁","17-22岁","22岁以上"};
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "石某某的年龄是"+items[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/17806c919df2821dafda565055329229.png)
三、多选对话框
public void mulite_dialog(View view) {
String[] items = {"张三","李四","王五","赵六"};
final boolean[] flags = {true,true,true,false};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("多选对话框");
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, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which] = isChecked;
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5db66f44d0f59582fea4b1814091f3c9.png)
四、自定义对话框
public void customer_dialog(View view) {
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.customer_dialog, null);
ImageView imageView = view1.findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view1);
AlertDialog dialog = builder.create();
dialog.show();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dbd19126fd4205c7eff30a5f687ff5e7.png)
五、水平进度条对话框
public void hor_progress_dialog(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("正在下载……");
progressDialog.setMax(100);
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+=5);
}
},0,1000);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ca05e1dd3379f56c76ed4708017eaa97.png)
六、圆形水平进度条对话框
public void square_progress_dialog(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
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);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ccf72a109456708536c78aff80d42ef.png)
七、日期选择对话框
public void date_choose_dialog(View view) {
Calendar calendar = 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();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4100e33a4fc1bd9f837c0f7e1af89a49.png)
八、时间选择对话框
public void time_choose_dialog(View view) {
Calendar instance = 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();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0de20254b84b64cac84d83089394494d.png)