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

Android实现单选与多选对话框的代码

程序员文章站 2024-02-19 21:54:28
android开发中实现单选与多选对话框的代码非常简单,具体代码如下所示: public void myclick(view view) { // 单选对话...

android开发中实现单选与多选对话框的代码非常简单,具体代码如下所示:

public void myclick(view view) {
  // 单选对话框
  //singlecheckdialog();
  // 多选对话框
  mulcheckdialog();
 }
 private void mulcheckdialog() {
  alertdialog.builder builder = new alertdialog.builder(this);
  builder.settitle("标题")
    .setmultichoiceitems(items, checkeditems, new dialoginterface.onmultichoiceclicklistener() {
     @override
     public void onclick(dialoginterface dialog, int which, boolean ischecked) {
     }
    })
    .setpositivebutton("确定", new dialoginterface.onclicklistener() {
     @override
     public void onclick(dialoginterface dialog, int which) {
      stringbuilder reuslt = new stringbuilder();
      for (int i = 0; i < items.length; i++) {
       if (checkeditems[i]) {
        reuslt.append(items[i]).append(" ");
       }
      }
      toast.maketext(mainactivity.this, "选择了" + reuslt.tostring(), toast.length_short).show();
     }
    })
    .setnegativebutton("取消", new dialoginterface.onclicklistener() {
     @override
     public void onclick(dialoginterface dialog, int which) {
     }
    }).show();
 }
 private void singlecheckdialog() {
  alertdialog.builder builder = new alertdialog.builder(this);
  builder.settitle("标题")
    .setsinglechoiceitems(items, -1, new dialoginterface.onclicklistener() {
     @override
     public void onclick(dialoginterface dialog, int which) {
      string itemstr = items[which];
      toast.maketext(mainactivity.this, "选择了" + itemstr, toast.length_short).show();
      // 关闭对话框
      //dialog.dismiss();
     }
    })
    .setpositivebutton("确定", new dialoginterface.onclicklistener() {
     @override
     public void onclick(dialoginterface dialog, int which) {
     }
    })
    .setnegativebutton("取消", new dialoginterface.onclicklistener() {
     @override
     public void onclick(dialoginterface dialog, int which) {
     }
    }).show();
 }

Android实现单选与多选对话框的代码
Android实现单选与多选对话框的代码

以上所述是小编给大家介绍的android实现单选与多选对话框的代码,希望对大家有所帮助