//创建建造者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("删除")
.setMessage("确定删除吗?");
// 设置确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "删除", 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();
}
});
//使用建造者 创建对话框
AlertDialog alertDialog = builder.create();
//显示
alertDialog.show();
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setPositiveButton("选择", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "111", Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "222", Toast.LENGTH_SHORT).show();
}
});
final String[] str=new String[]{"晴天","阴天","下雨天"};
//设置单选列表
//参数一 列表 参数二 默认选中下标 参数三 事件监听
builder1.setSingleChoiceItems(str, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中了"+str[which], Toast.LENGTH_SHORT).show();
}
});
//创建对话框
AlertDialog alertDialog1 = builder1.create();
//展示
alertDialog1.show();
final String[] items={"张三","李四","王五","赵六"};
//选中状态
final boolean[] flags={true,false,false,false};
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
//确认
builder2.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i
//创建建造者
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
//布局加载器加载需要的布局
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.diy_layout, null);
//找到布局控件
ImageView imageView = inflate.findViewById(R.id.image_id);
//设置自定义布局
builder3.setView(inflate);
final AlertDialog alertDialog3 = builder3.create();
//展示
alertDialog3.show();
//为自定义的控件事件
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog3.dismiss();
}
});
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("进度条");
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//定时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i=0;
@Override
public void run() {
//每秒增加20
i+=20;
if(i==100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(i);
}
},0,1000);
//确定监听
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "11111", Toast.LENGTH_SHORT).show();
}
});
//取消监听
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.show();
/**
* 上下文
* 监听
* 默认的年
* 默认的月
* 默认的日
*/
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();
/**
* 上下文
* 监听器
* 默认的时
* 默认的分钟
* 是否采用24小时
*/
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();
1.自定义类继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义的对象
public class diydialog extends Dialog{
private TextView diyTitleId;
private TextView diyMessageId;
private Button quxiaoId;
private Button quedingId;
private String titlestr,messagestr,yewstr,nostr;
private onNoOnclickListenter onNoOnclickListenter;
private onYesOnclickListener onYesOnclickListener;
public diydialog(Context context) {
super(context);
}
//取消的监听
public void setOnNoOnclickListenter(String str,onNoOnclickListenter onNoOnclickListenter){
if(str!=null){
nostr=str;
}
this.onNoOnclickListenter=onNoOnclickListenter;
}
//确定的监听
public void setOnYesOnclickListener(String str,onYesOnclickListener onYesOnclickListener){
if(str!=null){
yewstr=str;
}
this.onYesOnclickListener=onYesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diy1_layout);
setCanceledOnTouchOutside(false);
diyTitleId = findViewById(R.id.diy_title_id);
diyMessageId = findViewById(R.id.diy_message_id);
quxiaoId = findViewById(R.id.quxiao_id);
quedingId = findViewById(R.id.queding_id);
// 不为空时修改 为空不修改
if(titlestr!=null){
diyTitleId.setText(titlestr);
}
if(messagestr!=null){
diyMessageId.setText(messagestr);
}
if(yewstr!=null){
quedingId.setText(yewstr);
}
if(nostr!=null){
quxiaoId.setText(nostr);
}
//设置 点击 对外的监听
quxiaoId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onNoOnclickListenter!=null){
onYesOnclickListener.onYewClick();
dismiss();
}
}
});
quedingId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onYesOnclickListener!=null){
onYesOnclickListener.onYewClick();
dismiss();
}
}
});
}
//获取值
public void setTitlestr(String titlestr) {
this.titlestr = titlestr;
}
public void setMessagestr(String messagestr) {
this.messagestr = messagestr;
}
public interface onNoOnclickListenter{
public void onNoClick();
}
public interface onYesOnclickListener{
public void onYewClick();
}
}
2.MainActivity类
diydialog diydialog = new diydialog(this);
diydialog.setTitlestr("哈哈哈哈哈");
diydialog.setMessagestr("我是消息");
diydialog.setOnNoOnclickListenter("no", new diydialog.onNoOnclickListenter() {
@Override
public void onNoClick() {
}
});
diydialog.setOnYesOnclickListener("yes", new diydialog.onYesOnclickListener() {
@Override
public void onYewClick() {
Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
}
});
diydialog.show();