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

Android开发中的普通dialog实例解析

程序员文章站 2022-05-16 09:51:13
1.取消,确认 dialoginterface.onclicklistener dialogoncliclistener = new dialoginterface.onclicklisten...

1.取消,确认

 dialoginterface.onclicklistener dialogoncliclistener = new dialoginterface.onclicklistener() {

@override
public void onclick(dialoginterface dialog, int which) {
 switch (which) {
  case dialog.button_positive:
toast.maketext(getcontext(), "确认",
  toast.length_short).show();
break;
  case dialog.button_negative:
toast.maketext(getcontext(), "取消",
  toast.length_short).show();
break;
  case dialog.button_neutral:
toast.maketext(getcontext(), "忽略",
  toast.length_short).show();
break;
 }
}
  };

  alertdialog.builder builder = new alertdialog.builder(getcontext(),3);
  //builder.seticon(r.mipmap.ic_launcher);
  //builder.settitle("普通对话框");
  builder.setmessage("是否清除缓存?");
  builder.setpositivebutton("确认", dialogoncliclistener);
  builder.setnegativebutton("取消", dialogoncliclistener);
//  builder.setneutralbutton("忽略", dialogoncliclistener);
  builder.create().show();

2.列表

final string items[] = {"刘德华", "张柏芝", "蔡依林", "张学友"};

alertdialog.builder builder = new alertdialog.builder(this, 3);
builder.settitle("列表");
// builder.setmessage("是否确认退出?"); //设置内容
builder.seticon(r.mipmap.ic_launcher);
// 设置列表显示,注意设置了列表显示就不要设置builder.setmessage()了,否则列表不起作用。
builder.setitems(items, new dialoginterface.onclicklistener() {
 @override
 public void onclick(dialoginterface dialog, int which) {
  dialog.dismiss();
  toast.maketext(mainactivity.this, items[which],
 toast.length_short).show();

 }
});
builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
 @override
 public void onclick(dialoginterface dialog, int which) {
  dialog.dismiss();
  toast.maketext(mainactivity.this, "确定", toast.length_short)
 .show();
 }
});
builder.create().show();

3.单选

final string items[] = {"男", "女", "其他"};
alertdialog.builder builder = new alertdialog.builder(this,3);
builder.settitle("单选");
builder.seticon(r.mipmap.ic_launcher);
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();
}
  });
builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
 @override
 public void onclick(dialoginterface dialog, int which) {
  dialog.dismiss();
  toast.maketext(mainactivity.this, "确定", toast.length_short)
 .show();
 }
});
builder.create().show();

4.多选

final string items[] = {"java", "c++", "javascript", "mysql"};
  final boolean selected[] = {true, false, true, false};
  alertdialog.builder builder = new alertdialog.builder(this,3);
  builder.settitle("多选");
  builder.seticon(r.mipmap.ic_launcher);
  builder.setmultichoiceitems(items, selected,
 new dialoginterface.onmultichoiceclicklistener() {
  @override
  public void onclick(dialoginterface dialog, int which,
 boolean ischecked) {

toast.maketext(mainactivity.this,
  items[which] + ischecked, toast.length_short)
  .show();
  }
 });
  builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
 dialog.dismiss();
 toast.maketext(mainactivity.this, "确定", toast.length_short)
.show();
 // android会自动根据你选择的改变selected数组的值。
// for (int i = 0; i < selected.length; i++) {
//  log.e("hongliang", "" + selected[i]);
// }
}
  });
  builder.create().show();

5.可输入

final edittext edittext = new edittext(this);
final alertdialog.builder builder = new alertdialog.builder(this,3);
builder.settitle("可编辑");
builder.seticon(r.mipmap.ic_launcher);
builder.setview(edittext);
builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
 @override
 public void onclick(dialoginterface dialog, int which) {
  toast.maketext(mainactivity.this, edittext.gettext().tostring() + "", toast.length_long).show();
 }
});
builder.setnegativebutton("取消", new dialoginterface.onclicklistener() {
 @override
 public void onclick(dialoginterface dialog, int which) {
  dialog.cancel();
 }
});
builder.create().show();