在Android中,启动一个对话框有三种方式:

1、定义一个新的activity,并将其主题设置为对话框风格

2、使用AlertDialog类,并且显示它

3、使用 Android的Dialog类的子类,并且显示它

现在学习AlertDialog.Builder创建各种形式的对话框。

首先,看看启动界面如下:

用土司来显示效果,因为多次用到,所以将其抽象为一个方法。

  1. protected void showToast(String string) {
  2. Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
  3. }

1、点击第一个按钮后,出现如下对话框:

对于这个对话框,我们用到了AlertDialog.Builder类的几个方法:

setTitle:设置标题

setIcon:设置图标

setMessage:设置文本

setPositiveButton:设置第一个按钮

setNeutralButton:第二个按钮

setNegativeButton:第三个按钮

本段代码如下:

  1. public void onClick(View v) {
  2. switch (v.getId()) {
  3. case R.id.button1:
  4. new AlertDialog.Builder(this)
  5. .setTitle("这是一个最简单的对话框")
  6. .setIcon(R.drawable.img1)
  7. .setMessage("你好!!!")
  8. .setPositiveButton("开始", new DialogInterface.OnClickListener() {
  9. @Override
  10. public void onClick(DialogInterface dialog, int which) {
  11. DialogActivity.this.showToast("你点击了开始按钮      "+ which);
  12. }
  13. })
  14. .setNeutralButton("暂停", new DialogInterface.OnClickListener() {
  15. @Override
  16. public void onClick(DialogInterface dialog, int which) {
  17. DialogActivity.this.showToast("你点击了暂停按钮      "+ which);
  18. }
  19. })
  20. .setNegativeButton("退出", new DialogInterface.OnClickListener() {
  21. @Override
  22. public void onClick(DialogInterface dialog, int which) {
  23. DialogActivity.this.showToast("你点击了退出按钮 " + which);
  24. }
  25. }).show();
  26. break;

2、第二个对话框效果如下图

 

对于这个对话框,我们用到了这个方法

setItem(),即将setMessage改成这个方法就可以了。

代码如下:其中items是一个成员变量。final String[] items = {"开始","暂停","退出"};

  1. case R.id.button2:
  2. new AlertDialog.Builder(this)
  3. .setTitle("选项列表对话框")
  4. .setIcon(R.drawable.img2)
  5. //items的第一个参数也可以接受itemID,所以可写在xml文件中
  6. .setItems(items, new DialogInterface.OnClickListener() {
  7. @Override
  8. public void onClick(DialogInterface dialog, int which) {
  9. DialogActivity.this.showToast("你点击了按钮 " + items[which]);
  10. }
  11. }).show();
  12. break;

3、第三个对话框:

setSingleChoiceItem,设置单选列表对话框

代码如下:

  1. case R.id.button3:
  2. new AlertDialog.Builder(this)
  3. .setTitle("带单选框的列表对话框")
  4. .setIcon(R.drawable.img3)
  5. //setSingleChoiceItems()的第二个参数是设置默认选项,选项索引从0开始,-1代表不选择任何选项。
  6. .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
  7. @Override
  8. public void onClick(DialogInterface dialog, int which) {
  9. DialogActivity.this.showToast("你点击了按钮 " + items[which]);
  10. //需调用这个方法,使点击后对话框消失,不然一直不会消失的
  11. dialog.cancel();
  12. }
  13. }).create().show();
  14. break;

4、第四个对话框:

主要用到了方法:

setMultiChoiceItems

代码如下:

  1. case R.id.button4:
  2. new AlertDialog.Builder(this)
  3. .setTitle("你的爱好有:")
  4. .setIcon(R.drawable.img3)
  5. .setMultiChoiceItems(R.array.string_array_name, null, new DialogInterface.OnMultiChoiceClickListener() {
  6. @Override
  7. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  8. String[] array = DialogActivity.this.getResources().getStringArray(R.array.string_array_name);
  9. String str;
  10. if(isChecked) {
  11. number++;
  12. }else {
  13. number--;
  14. }
  15. DialogActivity.this.showToast(array[which] + (isChecked ?" 选中了":" 取消了") );
  16. }
  17. })
  18. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  19. @Override
  20. public void onClick(DialogInterface dialog, int which) {
  21. DialogActivity.this.showToast("你一共选择了      "+ number + "项");
  22. }
  23. }).show();
  24. break;

这次,并没有直接用数组,而是在strings.xml中定义的一个数组资源

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array  name="string_array_name">
  4. <item>健美操</item>
  5. <item>跳舞</item>
  6. <item>跑步</item>
  7. </string-array>
  8. </resources>

5、第5个对话框:

setView(view)方法来显示登录框。接受的参数为View(view,editText的组合),以LayoutInflater来实现。

要得到LayoutInflater(布局泵),只需要调用

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我们用inflater .inflater()用来找layout下xml布局文件,并且实例化。

类似于findVIewbyID,区别是一个得到整个布局,一个得到单个的组件。

代码如下:

  1. case R.id.button5:
  2. LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  3. View view = inflater.inflate(R.layout.dialog, null);
  4. new AlertDialog.Builder(this)
  5. .setTitle("登陆框")
  6. .setIcon(R.drawable.img4)
  7. .setView(view)
  8. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  9. @Override
  10. public void onClick(DialogInterface dialog, int which) {
  11. DialogActivity.this.showToast("正在登录,请稍后。。。");
  12. }
  13. }).show();

res/layout/dialog.xml布局为两个TextView,两个EditText,如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="账号"
  11. />
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/username"
  16. />
  17. <TextView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="密码"
  21. />
  22. <EditText
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:id="@+id/password"
  26. />
  27. </LinearLayout>

【转】http://blog.csdn.net/kuangc2008/article/details/6358915

安卓弹出对话框——AlertDialog(二)的更多相关文章

  1. 安卓弹出对话框——Alertdialog

    在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...

  2. 安卓弹出对话框——Alertdialog(一)

    首先看各种样式的对话框: 我们看到,Dialog有很多的子类实现,所以我们要定义一个对话框,使用其子类来实例化一个即可,而不要直接使用Dialog这个父类来构造. 二.AlertDialog 今天我们 ...

  3. 【Andriod-AlertDialog控件】 弹出对话框AlertDialog用法

    Result: Code: import android.app.Activity; import android.app.AlertDialog; import android.content.Di ...

  4. AlertDialog.Builder弹出对话框

    在Android中,弹出对话框使用AlertDialog.Builder方法. new AlertDialog.Builder(MainActivity.this).setTitle("本机 ...

  5. ViewPagerWithImageDemo【ViewPager如何判断滑动到第一页和最后一页以及弹出对话框功能】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录viewpager滑动的时候弹出对话框的功能(关键功能是滑动弹出对话框后,隐藏对话框的时候当前页可以还原到原位置),顺便判断首页 ...

  6. Response.Write("<script>alert('弹出对话框!')</script>") 后跟Response.Redirect("page.aspx");不能弹出对话框,直接跳转页面了 如何解?

    Response.Write和Response.Redirect一起用的时候就会这样,write脚本和redirect脚本不能同时使用,这样不会执行脚本,最好使用ClientScript 改进方法: ...

  7. selenium移动div里面的滚动条,操作弹出对话框

    还是使用js来移动 首先要定位到这个元素 倾向于使用js来定位元素,输入下面的脚本,按下回车键,即可在调试页面看到对应的div块$("div.table-responsive") ...

  8. Android 手机卫士--弹出对话框

    在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...

  9. service里面弹出对话框

    如何在service里面弹出对话框先给一个需求:需要在service里面监听短信的接收,如果接收到短信了,弹出一个dialog来提示用户打开. 看看效果图:(直接在主桌面上弹出) service中弹出 ...

随机推荐

  1. HDU 2444 The Accomodation of Students

    首先是要构造二分图,然后二分图的最大匹配. 还有没完全证明过我的方法的正确性,但是AC了..... #include<cstdio> #include<cstring> #in ...

  2. AS-->如何更高效的使用 Gradle, 快速build apk

    版权声明:欢迎转载,转载请注明出处;http://blog.csdn.net/angcyo 看本文之前,推荐先看我之前写的一篇文章: 传送门 日前Android Stuido 已经更新到 2.0.0 ...

  3. 一个web项目在myeclipse中add deployment时无法被识别出来的原因

    当我们一个web项目,在myeclipse中,add deployment时,可能发现,根本无法被识别成web项目,可能的原因有:   1. 项目的properties ->Myeclipse ...

  4. wind7系统修改host

    http://jingyan.baidu.com/article/e5c39bf56564a539d7603312.html 由于软件注册的原因,我需要更改hosts文件来防止服务器验证!那么在我们修 ...

  5. Popular Products

    Popular Products 描述 Given N lists of customer purchase, your task is to find the products that appea ...

  6. Recover Polygon (easy)

    Recover Polygon (easy) The zombies are gathering in their secret lair! Heidi will strike hard to des ...

  7. Android 联网监控抓包工具的制作(tcpdump的使用)

    最近做一个Android联网抓包的工具 自己在网上搜索了好久 发现还是没有头绪 于是考虑在linux层上下功夫 于是采用linux的tcpdump来实现了抓包的功能 用简单的话来定义tcpdump,就 ...

  8. Web安全检测工具的使用.

    Nikto2 Nikto2是一款使用perl语言写的多平台扫描软件,是一款命令行模式的工具,它可以扫描指定主机的WEB类型 主机名.特定目录.Cookie.特定CGI漏洞.XSS漏洞.sql注入漏洞. ...

  9. java.lang.RuntimeException: java.lang.NoSuchMethodException:

    [java] 15/12/19 14:09:46 INFO mapred.JobClient: Task Id : attempt_201512182036_0017_m_000000_0, Stat ...

  10. c# 添加了按钮双击事件后,再删除掉代码会提示错误

    有两种方法:.清空属性窗口中的双击事件(doubleclick )右边的内容: .单击“发生错误”提示窗口的“否”后,再双击错误列表里的错误项,此时编辑窗口跳转为xx.Designer.cs,然后注释 ...