一、确定取消选择对话框
// 单选对话框
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();
}
二、单选对话框
//多选对话框
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();
}
三、多选对话框
//多选对话框
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();
}
四、自定义对话框
// 自定义对话框
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();
}
五、水平进度条对话框
//水平进度条对话框
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);
}
六、圆形水平进度条对话框
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);
}
七、日期选择对话框
//日期选择对话框
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();
}
八、时间选择对话框
//时间选择对话框
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();
}
来源:CSDN
作者:Android程序员的一年成才记
链接:https://blog.csdn.net/qq_46041134/article/details/103722711