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

8种android 对话框(Dialog)使用方法详解

程序员文章站 2024-02-24 10:54:16
本文汇总了android 8种对话框(dialog)使用方法,分享给大家供大家参考,具体内容如下 1.写在前面 android提供了丰富的dialog函数,本文介绍最常...

本文汇总了android 8种对话框(dialog)使用方法,分享给大家供大家参考,具体内容如下

1.写在前面

android提供了丰富的dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮)、列表、单选、多选、等待、进度条、编辑、自定义等多种形式,将在第2部分介绍。
有时,我们希望在对话框创建或关闭时完成一些特定的功能,这需要复写dialog的create()、show()、dismiss()等方法,将在第3部分介绍。

2.代码示例

2.1 普通dialog(图1与图2)

2个按钮

public class mainactivity extends activity {
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    button buttonnormal = (button) findviewbyid(r.id.button_normal);
    buttonnormal.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        shownormaldialog();
      }
    });
  }
   
  private void shownormaldialog(){
    /* @seticon 设置对话框图标
     * @settitle 设置对话框标题
     * @setmessage 设置对话框消息提示
     * setxxx方法返回dialog对象,因此可以链式设置属性
     */
    final alertdialog.builder normaldialog = 
      new alertdialog.builder(mainactivity.this);
    normaldialog.seticon(r.drawable.icon_dialog);
    normaldialog.settitle("我是一个普通dialog")
    normaldialog.setmessage("你要点击哪一个按钮呢?");
    normaldialog.setpositivebutton("确定", 
      new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        //...to-do
      }
    });
    normaldialog.setnegativebutton("关闭", 
      new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        //...to-do
      }
    });
    // 显示
    normaldialog.show();
  }
}

3个按钮

/* @setneutralbutton 设置中间的按钮
 * 若只需一个按钮,仅设置 setpositivebutton 即可
 */
private void showmultibtndialog(){
  alertdialog.builder normaldialog = 
    new alertdialog.builder(mainactivity.this);
  normaldialog.seticon(r.drawable.icon_dialog);
  normaldialog.settitle("我是一个普通dialog").setmessage("你要点击哪一个按钮呢?");
  normaldialog.setpositivebutton("按钮1", 
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      // ...to-do
    }
  });
  normaldialog.setneutralbutton("按钮2", 
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      // ...to-do
    }
  });
  normaldialog.setnegativebutton("按钮3", new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      // ...to-do
    }
  });
  // 创建实例并显示
  normaldialog.show();
}

2.2 列表dialog(图3)

private void showlistdialog() {
  final string[] items = { "我是1","我是2","我是3","我是4" };
  alertdialog.builder listdialog = 
    new alertdialog.builder(mainactivity.this);
  listdialog.settitle("我是一个列表dialog");
  listdialog.setitems(items, new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      // which 下标从0开始
      // ...to-do
      toast.maketext(mainactivity.this, 
        "你点击了" + items[which], 
        toast.length_short).show();
    }
  });
  listdialog.show();
}

2.3 单选dialog(图4)

int yourchoice;
private void showsinglechoicedialog(){
  final string[] items = { "我是1","我是2","我是3","我是4" };
  yourchoice = -1;
  alertdialog.builder singlechoicedialog = 
    new alertdialog.builder(mainactivity.this);
  singlechoicedialog.settitle("我是一个单选dialog");
  // 第二个参数是默认选项,此处设置为0
  singlechoicedialog.setsinglechoiceitems(items, 0, 
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      yourchoice = which;
    }
  });
  singlechoicedialog.setpositivebutton("确定", 
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      if (yourchoice != -1) {
        toast.maketext(mainactivity.this, 
        "你选择了" + items[yourchoice], 
        toast.length_short).show();
      }
    }
  });
  singlechoicedialog.show();
}

2.4 多选dialog(图5)

arraylist<integer> yourchoices = new arraylist<>();
private void showmultichoicedialog() {
  final string[] items = { "我是1","我是2","我是3","我是4" };
  // 设置默认选中的选项,全为false默认均未选中
  final boolean initchoicesets[]={false,false,false,false};
  yourchoices.clear();
  alertdialog.builder multichoicedialog = 
    new alertdialog.builder(mainactivity.this);
  multichoicedialog.settitle("我是一个多选dialog");
  multichoicedialog.setmultichoiceitems(items, initchoicesets,
    new dialoginterface.onmultichoiceclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which,
      boolean ischecked) {
      if (ischecked) {
        yourchoices.add(which);
      } else {
        yourchoices.remove(which);
      }
    }
  });
  multichoicedialog.setpositivebutton("确定", 
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      int size = yourchoices.size();
      string str = "";
      for (int i = 0; i < size; i++) {
        str += items[yourchoices.get(i)] + " ";
      }
      toast.maketext(mainactivity.this, 
        "你选中了" + str, 
        toast.length_short).show();
    }
  });
  multichoicedialog.show();
}

2.5 等待dialog(图6)

private void showwaitingdialog() {
  /* 等待dialog具有屏蔽其他控件的交互能力
   * @setcancelable 为使屏幕不可点击,设置为不可取消(false)
   * 下载等事件完成后,主动调用函数关闭该dialog
   */
  progressdialog waitingdialog= 
    new progressdialog(mainactivity.this);
  waitingdialog.settitle("我是一个等待dialog");
  waitingdialog.setmessage("等待中...");
  waitingdialog.setindeterminate(true);
  waitingdialog.setcancelable(false);
  waitingdialog.show();
}

2.6 进度条dialog(图7)

private void showprogressdialog() {
  /* @setprogress 设置初始进度
   * @setprogressstyle 设置样式(水平进度条)
   * @setmax 设置进度最大值
   */
  final int max_progress = 100;
  final progressdialog progressdialog = 
    new progressdialog(mainactivity.this);
  progressdialog.setprogress(0);
  progressdialog.settitle("我是一个进度条dialog");
  progressdialog.setprogressstyle(progressdialog.style_horizontal);
  progressdialog.setmax(max_progress);
  progressdialog.show();
  /* 模拟进度增加的过程
   * 新开一个线程,每个100ms,进度增加1
   */
  new thread(new runnable() {
    @override
    public void run() {
      int progress= 0;
      while (progress < max_progress){
        try {
          thread.sleep(100);
          progress++;
          progressdialog.setprogress(progress);
        } catch (interruptedexception e){
          e.printstacktrace();
        }
      }
      // 进度达到最大值后,窗口消失
      progressdialog.cancel();
    }
  }).start();
}

2.7 编辑dialog(图8)

private void showinputdialog() {
  /*@setview 装入一个editview
   */
  final edittext edittext = new edittext(mainactivity.this);
  alertdialog.builder inputdialog = 
    new alertdialog.builder(mainactivity.this);
  inputdialog.settitle("我是一个输入dialog").setview(edittext);
  inputdialog.setpositivebutton("确定", 
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      toast.maketext(mainactivity.this,
      edittext.gettext().tostring(), 
      toast.length_short).show();
    }
  }).show();
}

2.8 自定义dialog(图9)

<!-- res/layout/dialog_customize.xml-->
<!-- 自定义view -->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <edittext
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    />
</linearlayout>

private void showcustomizedialog() {
  /* @setview 装入自定义view ==> r.layout.dialog_customize
   * 由于dialog_customize.xml只放置了一个editview,因此和图8一样
   * dialog_customize.xml可自定义更复杂的view
   */
  alertdialog.builder customizedialog = 
    new alertdialog.builder(mainactivity.this);
  final view dialogview = layoutinflater.from(mainactivity.this)
    .inflate(r.layout.dialog_customize,null);
  customizedialog.settitle("我是一个自定义dialog");
  customizedialog.setview(dialogview);
  customizedialog.setpositivebutton("确定",
    new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      // 获取editview中的输入内容
      edittext edit_text = 
        (edittext) dialogview.findviewbyid(r.id.edit_text);
      toast.maketext(mainactivity.this,
        edit_text.gettext().tostring(),
        toast.length_short).show();
    }
  });
  customizedialog.show();
}

3.复写回调函数

/* 复写builder的create和show函数,可以在dialog显示前实现必要设置
 * 例如初始化列表、默认选项等
 * @create 第一次创建时调用
 * @show 每次显示时调用
 */
private void showlistdialog() {
  final string[] items = { "我是1","我是2","我是3","我是4" };
  alertdialog.builder listdialog = 
    new alertdialog.builder(mainactivity.this){
     
    @override
    public alertdialog create() {
      items[0] = "我是no.1";
      return super.create();
    }
 
    @override
    public alertdialog show() {
      items[1] = "我是no.2";
      return super.show();
    }
  };
  listdialog.settitle("我是一个列表dialog");
  listdialog.setitems(items, new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
      // ...to-do
    }
  });
  /* @setondismisslistener dialog销毁时调用
   * @setoncancellistener dialog关闭时调用
   */
  listdialog.setondismisslistener(new dialoginterface.ondismisslistener() {
    public void ondismiss(dialoginterface dialog) {
      toast.maketext(getapplicationcontext(),
        "dialog被销毁了", 
        toast.length_short).show();
    }
  });
  listdialog.show();
}

以上就是本文的全部内容,希望对大家的学习有所帮助。