欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android基础之Dialog,通用属性设置

程序员文章站 2023-03-28 08:15:32
通用属性设置 设置对话框的标题内容等 //创建对话框,使用对话框构建器alertdialog.builder alertdialog.builder builder = new aler...

通用属性设置

设置对话框的标题内容等

 //创建对话框,使用对话框构建器alertdialog.builder
  alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
  //设置标题
  builder.settitle("标题");
  //设置对话框的图标
  builder.seticon(r.drawable.header);
  //设置内容
  builder.setmessage("内容");

设置确定取消按钮

 //设置确定按钮
  builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
 // todo auto-generated method stub
}
  });
  //设置取消按钮
  builder.setnegativebutton("取消", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
 // todo auto-generated method stub
}
  });

设置对话框显示与隐藏

  dialog = builder.create();
  //显示对话框
  dialog.show();
  //隐藏对话框
  dialog.dismiss();

设置点击按钮后不关闭对话框

field field = dialog.getclass().getsuperclass()
.getdeclaredfield("mshowing");
  field.setaccessible(true);
  //设置dialog显示/不显示
  field.set(dialog, true/false);

单选对话框

  //不能设置内容,否则单选效果无法展示
  //builder.setmessage("内容");
  //设置单选按钮
  string items1[] = new string[] { "魏", "蜀", "吴" };
  builder.setsinglechoiceitems(items1, 1, new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
 // todo auto-generated method stub
 toast.maketext(main3activity.this,which+"",toast.length_long).show();
}
  });

多选对话框

  string item_id2[] = new string[]{"魏", "蜀", "吴"};
  boolean checkeditems[] = {true, false, true};
  builder.setmultichoiceitems(item_id2, checkeditems, new dialoginterface.onmultichoiceclicklistener() {
@override
public void onclick(dialoginterface dialog, int which,
  boolean ischecked) {
 // todo auto-generated method stub
}
  });

进度对话框

**在android8.0以后使用progressbar代替progressdialog**

自定义view的对话框

  view view = view.inflate(main3activity.this, r.layout.item_dialog, null);
  final alertdialog.builder dialog = new alertdialog.builder(main3activity.this);
  dialog.settitle("用户登录:")
 .seticon(r.mipmap.ic_launcher)
 .setview(view)
 .setpositivebutton("ok", new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
toast.maketext(getapplicationcontext(), "登录成功", toast.length_short).show();
  }
 })

自定义view和位置的对话框

  view view = view.inflate(main3activity.this, r.layout.item_dialog, null);
  dialog dialog = new dialog(main3activity.this);
  //r.layout 后面跟的文件就是加载的自定义布局
  dialog.setcontentview(view);
  window mwindow = dialog.getwindow();
  mwindow.setgravity(gravity.left + gravity.top);
  windowmanager.layoutparams lp = mwindow.getattributes();
  lp.x = 0;
  lp.y = 0;
  lp.width = viewgroup.layoutparams.match_parent;
  lp.height = viewgroup.layoutparams.match_parent;
  mwindow.setattributes(lp);
  dialog.create();
  dialog.show();

日期和时间对话框

//日期对话框
datepickerdialog dialog_date = new datepickerdialog(mainactivity.this);
 dialog_date.settitle("选择日期:");
 dialog_date.seticon(r.mipmap.ic_launcher);
 dialog_date.setondatesetlistener(new datepickerdialog.ondatesetlistener() {
  @override
  public void ondateset(datepicker datepicker, int i, int i1, int i2) {
  toast.maketext(getapplicationcontext(), i + " " + i1 + "" + i2, toast.length_short).show();
  }
 });
 dialog_date.create();
 dialog_date.show();
//时间对话框(时。分)
timepickerdialog dialog_date = new timepickerdialog(mainactivity.this, new timepickerdialog.ontimesetlistener() {
  @override
  public void ontimeset(timepicker timepicker, int i, int i1) {
  toast.maketext(getapplicationcontext(), i + " " + i1, toast.length_short).show();
  }
 }, 18, 06, true);
 dialog_date.create();
 dialog_date.show();

布局和代码中使用

1,布局中:


  
  
  
2,代码中:
 //设置年月日监听器
 dp.init(dp.getyear(), dp.getmonth(), dp.getdayofmonth(), new datepicker.ondatechangedlistener() {
@override
public void ondatechanged(datepicker datepicker, int i, int i1, int i2) {
tv.settext("当前时间:"+ dp.getyear()+"-"+dp.getmonth()+"-"+dp.getdayofmonth()+"  "+tp.gethour()+":"+tp.getminute());
}
  });
  //设置时分监听器
  tp.setontimechangedlistener(new timepicker.ontimechangedlistener() {
@override
public void ontimechanged(timepicker timepicker, int i, int i1) {
tv.settext("当前时间:"+ dp.getyear()+"-"+dp.getmonth()+"-"+dp.getdayofmonth()+"  "+tp.gethour()+":"+tp.getminute());
}
  });