AlerDialog
自定义title,图标,显示内容,消极按钮setNegativeButton,中性按钮(可有可无)setNeutralButton,积极按钮setPositiveButton
//创建施工队
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("普通对话框");
//设置图标
builder.setIcon(R.mipmap.a);
builder.setMessage("对话框的测试内容");
//添加取消按钮
builder.setNegativeButton("消极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【取消】按钮", Toast.LENGTH_SHORT).show();
}
});
//添加中性按钮
builder.setNeutralButton("中性", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【中性】按钮", Toast.LENGTH_SHORT).show();
}
});
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【确定】按钮", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
MuliteDialog
setMultiChoiceItems,找到选中的位置在数据集合中相匹配
final boolean[] choose=new boolean[]{false,true,false,true,false,false};
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("你想成为哪个班委")
.setIcon(R.mipmap.a)
//设置多选列表项,试着勾选
.setMultiChoiceItems(items, choose, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
choose[which]=isChecked;
}
});
//添加取消按钮
builder.setNegativeButton("消极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【取消】按钮", Toast.LENGTH_SHORT).show();
}
});
//添加中性按钮
builder.setNeutralButton("中性", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【中性】按钮", Toast.LENGTH_SHORT).show();
}
});
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【确定】按钮", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
SingleDialog
监听器 .setSingleChoiceItems
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("单选对话框")
.setIcon(R.mipmap.a)
.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了"+items[which], Toast.LENGTH_SHORT).show();
}
});
//添加取消按钮
builder.setNegativeButton("消极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【取消】按钮", Toast.LENGTH_SHORT).show();
}
});
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【确定】按钮", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
ListDialog
设置适配器 setAdapter
int[] images={R.mipmap.a,R.mipmap.a,R.mipmap.a,R.mipmap.a,R.mipmap.a,R.mipmap.a};
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("自定义对话框")
.setIcon(R.mipmap.a)
.setAdapter(new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,items),null);
//添加消极按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【取消】按钮", Toast.LENGTH_SHORT).show();
}
});
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "单击了【确定】按钮", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
ProgresDialog
设置计时器,在计时器里写增删条件
new CountDownTimer(11000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
progressDialog.setProgress(a+=10);
}
@Override
public void onFinish() {
progressDialog.setMessage("已完成");
progressDialog.dismiss();
}
}.start();
TimeDialog
Calendar.getInstance() 获取当前时间
//获得当前时间
Calendar calendar = 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();
}
},calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
DataDialog
new DataPickerDialog 监听器
//获得当前时间
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+",日:"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
跳转到自定义Dialog,根据用户需要调用不同的方法
final MyDialog myDialog = new MyDialog(this);
myDialog.setTitle("提示");
myDialog.setMessage("确定退出应用?");
myDialog.setYesOnClickListener("确定", new MyDialog.onYesOnClickListener() {
@Override
public void onYesClickListener() {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
myDialog.dismiss();
}
});
myDialog.setNoOnClickListener("取消", new MyDialog.onNoOnClickListener() {
@Override
public void onNoClickListener() {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
myDialog.dismiss();
}
});
myDialog.show();
Window window = myDialog.getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.height=600;
attributes.width=600;
window.setGravity(Gravity.CENTER);
//设置透明背景
window.setBackgroundDrawableResource(android.R.color.transparent);
window.setAttributes(attributes);
ViewDialog
TableLayout 用布局填充器添加自定义布局